MongoDB Interview Questions For Beginners And Professionals

Recommended by 62 users

Mar 16, 2015
MongoDB Interview Questions For Beginners And Professionals
Add to Bookmark Email this Post 4.3K    1

Here’s a list of frequently asked MongoDB interview questions and answers for beginners as well as professionals.

What are the best features of Mongodb?

  • Document-oriented
  • High performance
  • High availability
  • Easy scalability
  • Rich-query language

When using replication, can some members use journaling and others not?

Yes!

Can journaling feature be used to perform safe hot backups?

Yes!

What is 32-bit nuances?

There is an extra memory mapped file activity with journaling. This will further constrain the limited db size of 32-bit builds. For now, journaling by default is disabled on 32-bit systems.

Will there be journal replay programs in case of incomplete entries (if there is a failure in the middle of one)?

Each journal (group) write is consistent and won’t be replayed during recovery unless it is complete.

What is the role of profiler in MongoDB?

MongoDB includes a database profiler which shows performance characteristics of each operation against the database. With this profiler you can find queries (and write operations) which are slower than they should be and use this information for determining when an index is needed.

What is 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’.

When an object attribute is removed, is it deleted from the store?

Yes, you can remove the attribute and then re-save() the object.

Are null values allowed?

Yes, but only for the members of an object. A null cannot be added to the database collection as it isn’t an object. But {}can be added.

Does an update fsync to disk immediately?

No. Writes to disk are lazy by default. A write may only hit the disk a couple of seconds later. For example, if the database receives thousand increments to an object within one second, it will only be flushed to disk once. (Note: fsync options are available both at the command line and via getLastError_old.)

How do I do transactions/locking?

MongoDB does not use traditional locking or complex transactions with rollback, as it is designed to be light weight, fast and predictable in its performance. It can be thought of how analogous is to the MySQL’s MyISAM autocommit model. By keeping transaction support extremely simple, performance is enhanced, especially in a system that may run across many servers.

Why are data files so large?

MongoDB does aggressive preallocation of reserved space to avoid file system fragmentation.

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 to be elected. During this window of time, the cluster is down for primary operations i.e writes and strong consistent reads. However, eventually consistent queries may be executed to secondaries at any time (in slaveOk mode), including during this window.

What’s a Master or Primary?

This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.

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).

Is it required to call ‘getLastError’ to make a write durable?

No. If ‘getLastError’ (aka ‘Safe Mode’) is not called, the server does exactly behave the way as if it has been called. The ‘getLastError’ call simply allows one to get a 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.

Should you start out with Sharded or with a Non-Sharded MongoDB environment?

We suggest starting with Non-Sharded for simplicity and quick startup, unless your initial data set will not fit on single servers. Upgrading to Sharded from Non-sharded is easy and seamless, so there is not a lot of advantage in setting up Sharding before your data set is large.

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. Using a replica set for each Shard is highly recommended.

When will data be on more than one Shard?

MongoDB Sharding is range-based. So all the objects in a collection lie into a chunk. Only when there is more than 1 chunk there is an option for multiple Shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for migration.

What happens when a document is updated 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.

What happens when a Shard is down or slow when querying?

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.

Can the old files in the ‘moveChunk’ directory be removed?

Yes, these files are made as backups during normal Shard balancing operations. Once the operations are done then they can be deleted. The clean-up process is currently manual so this needs to be taken care of to free up space.

How do you see the connections used by Mongos?

The following command needs to be used: db._adminCommand(“connPoolStats”);

If a ‘moveChunk’ fails, is it necessary to cleanup the partially moved docs?

No, chunk moves are consistent and deterministic. The move will retry and when completed, the data will be only on the new Shard.

What are the disadvantages of MongoDB?

  • A 32-bit edition has 2GB data limit. After that it will corrupt the entire DB, including the existing data. A 64-bit edition won’t suffer from this bug/feature.
  • Default installation of MongoDB has asynchronous and batch commits turned on. Meaning, it lies when asked to store something in DB and commits all changes in a batch at a later time in future. If there is a server crash or power failure, all those commits buffered in memory will be lost. This functionality can be disabled, but then it will perform as good as or worse than MySQL.
  • MongoDB is only ideal for implementing things like analytics/caching where impact of small data loss is negligible.
  • In MongoDB, it’s difficult to represent relationships between data so you end up doing that manually by creating another table to represent the relationship between rows in two or more tables.

Got a question for us? Please mention them in the comments section and we will get back to you. 

Related Posts: 

MongoDB – The Database to Bank on

Understanding MongoDB Architecture

MongoDB Installation on Ubuntu

MongoDB Development & Administration Training

Share on
Comments
1 Comment

24 X 7 Customer Support X

  • us flag 1-800-275-9730 (Toll Free)
  • india flag +91 88808 62004