28 MongoDB NoSQL Database Interview Questions and Answers
28 MongoDB NoSQL Database Interview Questions and Answers
MongoDB is the best free open source NoSQL document oriented database. If you are preparing for the technical interview on MongoDB NoSQL database, must prepare the following MongoDB NoSQL database interview questions. These MongoDB NoSQL database interview questions cover basic concepts of NoSQL databases, Replication, Sharding, Transactions and Locking, Profiler, Nuances, Journalling features etc. Lets have a look on following MongoDB NoSQL database interview questions:
1. What do you mean by NoSQL databases? What is the difference between NoSQL and RDBMS? Why and why not to use NoSQL databases? What are the various advantages of NoSQL databases?
I have written a complete blog post to answer above questions here.
Examples: MongoDB, Cassandra, CouchDB, Hypertable, Redis, Riak, Neo4j, HBASE, Couchbase, MemcacheDB, RevenDB and Voldemort are the examples of NoSQL databases. Read in detail.
3. What is the basic difference between MySQL and MongoDB?
MySQL and MongoDB are both free and open source databases. MySQL and MongoDB have a lot of basic differences in terms of data representation, querying, relationships, transactions, schema design and definition, normalization, speed and performance. By comparing MySQL with MongoDB, we are comparing Relational and non-relational databases. Read in detail.
4. How do you compare MongoDB with CouchDB and CouchBase?
MongoDB and CouchDB are both document-oriented databases. MongoDB and CouchDB are the best examples of open source NoSQL database. Aside from both storing documents though, it turns out that they don't share much in common. There are a lot of difference between MongoDB and CouchDB in terms of implementation of their data-model, interface, object storage, replication methods etc.
For details, go through following links:
5. What makes MongoDB best?
Following features of MongoDB make it best NoSQL database:
Document-oriented
High performance
High availability
Easy scalability
Rich query language
6. What is 32 bit nuances?
There is extra memory mapped file activity with journaling. This will further constrain the limited db size of 32 bit builds. Thus, for now journaling by default is disabled on 32 bit systems.
7. Will the journal replay have problems if entries are incomplete (like the failure happened in the middle of one)?
Each journal (group) write is consistent and won't be replayed during recovery unless it is complete.
8. What is role of Profiler in MongoDB?
MongoDB includes a database profiler which shows performance characteristics of each operation against the database. Using the profiler you can find queries (and write operations) which are slower than they should be; use this information, for example, to determine when an index is needed.
9. What's a "namespace"?
MongoDB stores BSON objects in collections. The concatenation of the database name and the collection name (with a period in between) is called a namespace.
10. If you remove an object attribute is it deleted from the store?
Yes, you remove the attribute and then re-save() the object.
11. Can I use the journaling feature to perform safe hot backups?
Yes
12. Are null values allowed?
For members of an object, yes. You cannot add null to a database collection though as null isn't an object. You can add {}, though.
13. Does an update fsync to disk immediately?
No, writes to disk are lazy by default. A write may hit disk a couple of seconds later. For example, if the database receives a thousand increments to an object within one second, it will only be flushed to disk once. (Note fsync options are available though both at the command line and via getLastError_old.)
14. How do I do transactions/locking?
MongoDB does not use traditional locking or complex transactions with rollback, as it is designed to be lightweight and fast and predictable in its performance. It can be thought of as analogous to the MySQL MyISAM autocommit model. By keeping transaction support extremely simple, performance is enhanced, especially in a system that may run across many servers.
15. Why are my data files so large?
MongoDB does aggressive preallocation of reserved space to avoid file system fragmentation.
16. How long does replica set failover take?
It may take 10-30 seconds for the primary to be declared down by the other members and a new primary elected. During this window of time, the cluster is down for "primary" operations – that is, writes and strong consistent reads. However, you may execute eventually consistent queries to secondaries at any time (in slaveOk mode), including during this window.
17. What's a master or primary?
This is a node/member which is currently the primary and processes all writes for the replica set. In a replica set, on a failover event, a different member can become primary.
18. What's a secondary or slave?
A secondary is a node/member which applies operations from the current primary. This is done by tailing the replication oplog (local.oplog.rs).
Replication from primary to secondary is asynchronous, however the secondary will try to stay as close to current as possible (often this is just a few milliseconds on a LAN).
19. Do I have to call getLastError to make a write durable?
No. If you don't call getLastError (aka "Safe Mode") the server does exactly the same behavior as if you had. The getLastError call simply lets one get confirmation that the write operation was successfully committed. Of course, often you will want that confirmation, but the safety of the write and its durability is independent.
20. Should I start out with sharded or with a non-sharded MongoDB environment?
We suggest starting unsharded for simplicity and quick startup unless your initial data set will not fit on single servers. Upgrading to sharding from unsharded is easy and seamless, so there is not a lot of advantage to setting up sharding before your data set is large.
21. How does sharding work with replication?
Each shard is a logical collection of partitioned data. The shard could consist of a single server or a cluster of replicas. We recommmend using a replica set for each shard.
22. When will data be on more than one shard?
MongoDB sharding is range based. So all the objects in a collection get put into a chunk. Only when there is more than 1 chunk is there an option for multiple shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for a migration to occur.
23. What happens if I try to update a document on a chunk that is being migrated?
The update will go through immediately on the old shard, and then the change will be replicated to the new shard before ownership transfers.
24. What if a shard is down or slow and I do a query?
If a shard is down, the query will return an error unless the "Partial" query options is set. If a shard is responding slowly, mongos will wait for it.
25. Can I remove old files in the moveChunk directory?
Yes, these files are made as backups during normal shard balancing operations. Once the operations are done then they can be deleted. The cleanup process is currently manual so please do take care of this to free up space.
26. How can I see the connections used by mongos?
db._adminCommand("connPoolStats");
27. If a moveChunk fails do I need to cleanup the partially moved docs?
No, chunk moves are consistent and deterministic; the move will retry and when completed the data will only be on the new shard.
28. If I am using replication, can some members use journaling and others not?
Yes