Mongo-DB Interview Questions and Answers

Q : Explain what is MongoDB?
A : Mongo-DB is a document database which provides high performance, high availability and easy scalability.

Q : What is sharding in MongoDB?
A : 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.

Q : What is “Namespace” in MongoDB?
A : 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.

Q : How can you see the connection used by Mongos?
A : To see the connection used by Mongos use db_adminCommand (“connPoolStats”);

Q : How replication works in MongoDB?
A : 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.

Q : Explain what is a replica set?
A : 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.

Q : While creating Schema in MongoDB what are the points need to be taken in consideration?
A : 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

Q : Explain what is the role of profiler in MongoDB?
A : 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.

Q : What is the syntax to create a collection and to drop a collection in MongoDB?
A :

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

Q : To do safe backups what is the feature in MongoDB that you can use?
A : Journaling is the feature in MongoDB that you can use to do safe backups.

Q : Explain can you move old files in the moveChunk directory?
A : 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.

Q : Mention what is ObjectId composed of?
A : ObjectId is composed of

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

Q : Mention how you can inspect the source code of a function?
A : To inspect a source code of a function, without any parentheses, the function must be invoked.

Q : Mention what is the command syntax for inserting a document?
A : For inserting a document command syntax is database.collection.insert (document).

Q : Explain what are indexes in MongoDB?
A : 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.

Q : What is the command syntax that tells you whether you are on the master server or not? And how many master does MongoDB allow?
A : 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.

Q : Explain what is GridFS in MongoDB?
A : 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.

Q : Mention the command syntax that is used to view Mongo is using the link?
A : The command syntax that is used to view mongo is using the link is db._adminCommand(“connPoolStats.”)

Q : Mention what is the basic syntax to use index in MongoDB?
A : The basic syntax to use in MongoDB is >db.COLLECTION_NAME.ensureIndex ( {KEY:1} ). In here the key is the the name of the COLUMN (or KEY:VALUE pair) which is present in the documents.

Q : What are alternatives to MongoDB?
A : Cassandra, CouchDB, Redis, Riak, Hbase are a few good alternatives.