• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

MongoDB Interview Questions and Answers

1) Explain what is MongoDB?

Mongo-DB is a document database which provides high performance, high availability and easy scalability.

2) What is “Namespace” in MongoDB?

MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the collection. The concatenation of the collection name and database name is called a namespace.

3) What is sharding in MongoDB?

The procedure of storing data records across multiple machines is referred as Sharding. It is a MongoDB approach to meet the demands of data growth. It is the horizontal partition of data in a database or search engine. Each partition is referred as shard or database shard.

4) How can you see the connection used by Mongos?

To see the connection used by Mongos use db_adminCommand (“connPoolStats”);

5) Explain what is a replica set?

A replica set is a group of mongo instances that host the same data set. In replica set, one node is primary, and another is secondary. From primary to the secondary node all data replicates.

6) How replication works in MongoDB?

Across multiple servers, the process of synchronizing data is known as replication. It provides redundancy and increase data availability with multiple copies of data on different database server. Replication helps in protecting the database from the loss of a single server.

7) While creating Schema in MongoDB what are the points need to be taken in consideration?

Points need to be taken in consideration are

– Design your schema according to user requirements
– Combine objects into one document if you use them together. Otherwise, separate them
– Do joins while write, and not when it is on read
– For most frequent use cases optimize your schema
– Do complex aggregation in the schema

8) What is the syntax to create a collection and to drop a collection in MongoDB?

– Syntax to create collection in MongoDB is db.createCollection(name,options)
– Syntax to drop collection in MongoDB is db.collection.drop()

9) Explain what is the role of profiler in MongoDB?

MongoDB database profiler shows performance characteristics of each operation against the database. You can find queries using the profiler that are slower than they should be.

10) Explain can you move old files in the moveChunk directory?

Yes, it is possible to move old files in the moveChunk directory, during normal shard balancing operations these files are made as backups and can be deleted once the operations are done.

11) To do safe backups what is the feature in MongoDB that you can use?

Journaling is the feature in MongoDB that you can use to do safe backups.

12) Mention what is Objecld composed of?

Objectld is composed of

  • Timestamp
  • Client machine ID
  • Client process ID
  • 3 byte incremented counter

13) Mention what is the command syntax for inserting a document?

For inserting a document command syntax is database.collection.insert (document).

14) Mention how you can inspect the source code of a function?

To inspect a source code of a function, without any parentheses, the function must be invoked.

15) What is the command syntax that tells you whether you are on the master server or not? And how many master does MongoDB allow?

Command syntax Db.isMaster() will tell you whether you are on the master server or not. MongoDB allows only one master server, while couchDB allows multiple masters.

16) Mention the command syntax that is used to view Mongo is using the link?

The command syntax that is used to view mongo is using the link is db._adminCommand(“connPoolStats.”)

17) Explain what are indexes in MongoDB?

Indexes are special structures in MongoDB, which stores a small portion of the data set in an easy to traverse form. Ordered by the value of the field specified in the index, the index stores the value of a specific field or set of fields.

18) Mention what is the basic syntax to use index in MongoDB?

The basic syntax to use in MongoDB is >db.COLLECTION_NAME.ensureIndex ( {KEY:1} ). In here the key is the name of the file on which you want to create an index, where 1 is for ascending order while you use -1 for descending order.

19) Explain what is GridFS in MongoDB?

For storing and retrieving large files such as images, video files and audio files GridFS is used. By default, it uses two files fs.files and fs.chunks to store the file’s metadata and the chunks.

20) What are alternatives to MongoDB?

Cassandra, CouchDB, Redis, Riak, HBase are a few good alternatives.

21) How long does replica set fail over 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.

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

23) Should I start out with sharded or with a non-shared MongoDB environment?

We suggest starting unshared for simplicity and quick startup unless your initial data set will not fit on single servers. Upgrading to sharding from unshared is easy and seamless, so there is not a lot of advantage to setting up sharing before your data set is large.

Learn more about Mongo DB Interview Questions in this blog post.
Interested in mastering Mongo DB? 
Check out this blog post to learn more Mongo DB Tutorials.

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

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

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

27) How can I see the connections used by mongos?

db._adminCommand("connPoolStats");

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

29) What makes Mongodb best?

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

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

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.

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

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

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

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

35) 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 sharing from unshared is easy and seamless, so there is not a lot of advantage to setting up sharing before your data set is large.

For indepth understanding click on

Summary
Review Date
Reviewed Item
MongoDB Interview Questions and Answers
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on MongoDB Interview Questions and Answers"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.