MongoDB is a document-based database created in 2007 by 10gen software. It works on the concept of collections and documents. A MongoDB server can contain multiple databases and offers high performance along with redundancy and easy scalability. Collection in MongoDB can be considered as a group of documents that can have different types of fields. The document is a set of key-value pairs having a dynamic schema i.e. common fields may hold different types of data and all documents in the same collection need not have the same structure. If you are new to MongoDB but have worked with RDBMS like SQL before, this small table will help you correlate the terminologies –

MongoDB Interview Questions

RDBMSMongoDB
TableCollection
RowDocument
ColumnField
Table joinEmbedded documents

A few prominent features of MongoDB are –

  • Cross-platform
  • Written in C++.
  • Secure and scalable
  • Schema-less database
  • Document-oriented
  • Supports high-availability and redundancy

Commonly asked basic and advanced interview questions

Question: What is Mongo shell?

Answer: Mongo shell is a JavaScript interface to MongoDB that can be used to query and update data. It is interactive and can also be used to do administrative operations.

Question: How does MongoDB store data?

Answer: Being document-based, MongoDB stores documents in BSON or Binary JavaScript Object notation which is the binary encoded format of JSON.

Question: What type of NoSQL database is MongoDB?

Answer: It is a document database which contains documents having key-value pairs, key-array pairs, and nested documents.

Question: List out the important features of MongoDB.

Answer: Important features of MongoDB are –

  • It is a schema-less database
  • No complex joins
  • Faster access to data because of the presence of the working set (internal memory)
  • Features like aggregation, sharding, and replication make it easy to use.
  • Cross-platform and document based
  • Automatic fail-over and high-availability

Question: What are the data models in MongoDB?

Answer: Data modeling depends on the structure of documents. In MongoDB,

  • Related data can be embedded in a single document structure (Embedded data model)
  • Through references from one document to another, the relationship between data is stored (Normalized data model)

Question: MongoDB is called a schema-less database. Is it true? How to create the schema in MongoDB?

Answer: It would be more appropriate to say that MongoDB has dynamically typed schema because it relies on JSON which is a schema-free data structure. To create a schema, create and insert a document. Once a document is inserted, a corresponding collection will be created in the database.

Question: What is a namespace in MongoDB?

Answer: A namespace is the concatenation of the database name and collection name. Example – students.the subject, where students are the database and subject, is the collection.

Question: How do you perform CRUD operations in MongoDB?

Answer: 

  • C – Create – db.collection.insert();
  • R – Read – db.collection.find();
  • U – Update – db.collection.update();
  • D – Delete – db.collection.remove({“fieldname” : ”value”});

Question: What are the differences between MySQL and MongoDB?

Answer: 

MySQLMongoDB
Written in C and C++Written in C++ and JavaScript
Follows RDBMS database structureDocument-based structure
Scaling is done verticallyHorizontal scaling
Rigidly defined data-structure, the schema is strict and should be defined in the beginning.Dynamic and flexible schema creation of complex documents.
Uses a Structured Query languageThe unstructured query language is used.
Uses Join to link data from two or more tablesThere is no equivalent for JOIN.
Performance is slow with a large database with unstructured data.Handles large unstructured data efficiently thereby giving a good performance.
Less suitable for a cloud-based environmentThe best option for services that are cloud-based.

Question: How are constraints managed in MongoDB?

Answer: You can add document validator on the collections starting MongoDB 3.2. Unique indexes can also be created using db.collection.createIndex({“key” : 1} , {unique : true});

Question: What is ObjectId? How is it structured?

Answer: ObjectId is a class which is the default primary key for the document in MongoDB. It is found in the _id field of the inserted document. It is a 12-byte BSON type generated using a default algorithm. The structure is –

  • A 4-byte value that represents seconds since Unix epoch
  • 3-byte machine id
  • 2-byte process id
  • 3-bytes counter that starts with a random number

Question: What is BSON?

Answer: BSON or binary JSON is the binary encoded format of JSON. It extends JSON and provides more data types and fields.

Question: How does MongoDB do indexing?

Answer: Indexing is done using ensureIndex() method. Syntax –

db.COLLECTION_NAME.ensureIndex({KEY:1})

Question: Name the default index created for a new collection.

Answer: Default index created for the new collection is _id

Question: What is sharding in MongoDB?

Answer: MongoDB meets the data growth using Sharding, which means sorting data records across various machines. Each shard is a replica set.

Question: What is a covered query and why is it important?

Answer: In a covered query, all the fields used in the query have the index created. The results returned should also be part of the index.  Due to this, MongoDB fetches the results without actually looking inside documents, thus saving time and increasing efficiency.

Question: What is replication and what are the primary and secondary replica sets?

Answer: Replication means synchronizing the data across multiple servers. It increases data availability. If a single server is lost, data is still intact in the other servers.

  • Primary replica set – MongoDB writes data only to primary or master replica set.
  • Secondary replica set – secondary or slave nodes can accept only reads. They replicate from the primary.

Question: Name the 2 storage engines using MongoDB?

Answer: MMAPv1 and WiredTiger are the 2 storage engines used by MongoDB.

Question: How to create and drop a collection in MongoDB?

Answer:

  • Create collection – db.createCollection();
  • Drop collection – db.collection.drop();

Question: What is journaling and how does it work?

Answer: MongoDB ensures data integrity by using an on-disk journal that is created for every write. In case of a server crash, the journal can be used to track the writes that were not written to the disk or data files.

Question: How can you store images, videos and other large files in MongoDB?

Answer: Large files are stored in MongoDB using the GridFS specification.

Question: Compare CouchDB and MongoDB.

Answer:

CouchDBMongoDB
The data model is document oriented i.e. JSONUses BSON which is an extension of JSON
Uses HTTP/REST for queryingUses standard protocol over TCP/IP
Database has documentsThe database contains collections and collections contain documents
Written in ErlangWritten in C++
Uses range queries and map/reduceUses object-based query language and map/reduce

Question: In what ways is MongoDB better than MySQL? Explain.

Answer: MongoDB has a flexible data model wherein the schema can be expanded based on business needs. Also, MongoDB is faster and can handle more data types to manage real-time applications better.

Question: What are the different index types in MongoDB?

Answer: 

  • Default – this is the _id that MongoDB creates
  • Single field – for indexing and sorting on a single field
  • Compound – for multiple fields
  • Multi-key – for indexing array data
  • Hashed – indexes the hashes of a field value
  • Geospatial – to query geospatial(location) data

Question: What is an ACID transaction? Does MongoDB support it?

Answer: ACID stands for Atomicity, Consistency, Isolation, and Durability. The transaction manager ensures these attributes are taken care of. Yes, MongoDB version 4.0 supports ACID.

Question: Explain the role of profiler in MongoDB.

Answer:  A profiler is an in-built tool that finds out the slow and resource-intensive queries and provides query-level insights. You as a dba can analyze the queries. Profiler stores all the data in system.profile collection.

Question: How does MongoDB handle transaction and locks?

Answer: MongoDB uses multi-granularity locking wherein operations can be locked at the global, database or collection level. It is up to the storage engines to implement the level of concurrency. For example, in WiredTiger, it is at document-level. For reads, there is a shared locking mode, while for write there is an exclusive locking mode.

Question: What is the command to list all the indexes in a collection?

Answer: The command is db.collection.getIndexes();

Question: How does MongoDB do text search?

Answer: Text search can be done using text index. Example –

db.collection_name.ensureIndex({field:”text”});

Question: What is the default interval to write updates to the disk?

Answer: The default interval is 60 seconds.

Question: Explain aggregation in MongoDB.

Answer: Aggregation groups values from different documents and performs operations on the data to return a single result. There are 3 ways in which MongoDB performs aggregation –

  • Aggregation pipeline
  • Map-reduce function
  • Single purpose aggregation methods

Question: State the difference between find() and limit() method.

Answer: find() – displays only selected data rather than all the data of a document. Example, if your document has 4 fields but you want to show only one, set the required field as 1 and others as 0.

db.COLLECTION_NAME.find({},{KEY:1});

limit() – limit function limits the number of records fetched. For example, if you have 7 documents but want to display only the first 4 documents in a collection, use limit. Syntax –

db.COLLECTION_NAME.find().limit(NUMBER);

Question: How does concurrency affect primary replica set?

Answer: When the collection changes are written to primary, MongoDB writes the same to a special collection in the local database, called the primary’s oplog. Hence, both the collection’s database and local database are locked.

Question: Give an example of insert, delete and update statement in MongoDB.

Insert
db.books.insert({
   _id: ObjectId(7df74fd8902c),
   title: 'All about MongoDB', 
   description: 'Everything you need to know about MongoDB',
   by: 'Author1',
   url: 'http://www.blogsuthor1.com'
})
DeleteRemove a particular document –

 db.books.remove({'title':'All about MongoDB'})

Remove all the documents

db.books.remove();
UpdateTo update values of an existing document –

db.books.update({'title':'All 
about MongoDB'},{$set:{'title':'MongoDB for dummies'}})

To replace an existing document with a new
one based on ObjectId –

db.books.save(
   {
      "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB for dummies",
      "by":"Author2"
   }
)

Question: What have capped collections?

Answer: Capped collections are fixed-size collection and insert and retrieve data based on insertion order. If a collection’s space is full, the oldest records will be overwritten by the new documents in the collection.

Question: Explain the purpose of map-reduce command in Mongo-DB.

Answer: Map-reduce is a method of performing aggregation.

  • Map function emits key-value pair specified.
  • Reduce function combines the key value pair and returns the aggregation result.

Syntax –

db.collection.mapReduce( 
       function() {emit(key,value);}, 
       function(key, values) {return aggregatedResult}, { out: collection } 
</pre.
    ) 

Question: List some of the data types supported by MongoDB.

Answer: Some data types are Numbers, String, Array, Binary data, Boolean, Date, Regular expressions, ObjectId, etc…

Question: How can applications access real-time data changes in MongoDB?

Answer: Applications can access real-time data changes using Change streams which acts as a subscriber to all the collection operations like insert, delete and update.

Question: What are the commands to create a backup of the data and restore the data?

Answer: Use the command mongodump to create a backup.

Use the command mongorestore [backup_path] to restore the data.

Question: How do you refer to the current database in a statement? (by using db)

Answer: Current database is referred to as ‘db’. Example – db.collection.find();

People Also Read: