300+ TOP MongoDB Interview Questions and Answers

MongoDB Interview Questions for freshers experienced :-

1. What is MongoDB?

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

2. Which are the different languages supported by MongoDB?
MonggoDB provides official driver support for C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go and Erlang.

You can use MongoDB with any of the above languages. There are some other community supported drivers too but the above mentioned ones are officially provided by MongoDB.

3. What are the different types of NoSQL databases? Give some example.
NoSQL database can be classified as 4 basic types:

  1. Key value store NoSQL database
  2. Document store NoSQL database
  3. Column store NoSQL database
  4. Graph base NoSQL databse

There are many NoSQL databases. MongoDB, Cassandra, CouchBD, Hypertable, Redis, Riak, Neo4j, HBASE, Couchbase, MemcacheDB, Voldemort, RevenDB etc. are the examples of NoSQL databases.

4. Is MongoDB better than other SQL databases? If yes then how?
MongoDB is better than other SQL databases because it allows a highly flexible and scalable document structure.

For example:

  • One data document in MongoDB can have five columns and the other one in the same collection can have ten columns.
  • MongoDB database are faster than SQL databases due to efficient indexing and storage techniques.

5. What type of DBMS is MongoDB?
MongoDB is a document oriented DBMS

6. What is the difference between MongoDB and MySQL?
Although MongoDB and MySQL both are free and open source databases, there is a lot of difference between them in the term of data representation, relationship, transaction, querying data, schema design and definition, performance speed, normalization and many more. To compare MySQL with MongoDB is like a comparison between Relational and Non-relational databases.

7. Why MongoDB is known as best NoSQL database?
MongoDb is the best NoSQL database because, it is:

  • Document Oriented
  • Rich Query language
  • High Performance
  • Highly Available
  • Easily Scalable

8. Does MongoDB support primary-key, foreign-key relationship?
No. By Default, MongoDB doesn’t support primary key-foreign key relationship.

9. Can you achieve primary key – foreign key relationships in MongoDB?
We can achieve primary key-foreign key relationship by embedding one document inside another. For example: An address document can be embedded inside customer document.

10. Does MongoDB need a lot of RAM?
No. There is no need a lot of RAM to run MongoDB. It can be run even on a small amount of RAM because it dynamically allocates and de-allocates RAM according to the requirement of the processes.

MongoDB Interview Questions
MongoDB Interview Questions

11. Explain the structure of ObjectID in MongoDB.
ObjectID is a 12-byte BSON type. These are:

  • 4 bytes value representing seconds
  • 3 byte machine identifier
  • 2 byte process id
  • 3 byte counter

12. Is it true that MongoDB uses BSON to represent document structure?
Yes.

13. What are Indexes in MongoDB?
In MondoDB, Indexes are used to execute query efficiently. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

14. By default, which index is created by MongoDB for every collection?
By default, the_id collection is created for every collection by MongoDB.

15. What is a Namespace in MongoDB?
Namespace is a concatenation of the database name and the collection name. Collection, in which MongoDB stores BSON objects.

16. Can journaling features be used to perform safe hot backups?
Yes.

17. Why does Profiler use in MongoDB?
MongoDB uses a database profiler to perform characteristics of each operation against the database. You can use a profiler to find queries and write operations

18. If you remove an object attribute, is it deleted from the database?
Yes, it be. Remove the attribute and then re-save(. the object.

19. In which language MongoDB is written?
MongoDB is written and implemented in C++.

20. Does MongoDB need a lot space of Random Access Memory (RAM)?
No. MongoDB can be run on small free space of RAM.

21. What language you can use with MongoDB?
MongoDB client drivers supports all the popular programming languages so there is no issue of language, you can use any language that you want.

22. Does MongoDB database have tables for storing records?
No. Instead of tables, MongoDB uses “Collections” to store data.

23. Do the MongoDB databases have schema?
Yes. MongoDB databases have dynamic schema. There is no need to define the structure to create collections.

24. What is the method to configure the cache size in MongoDB?
MongoDB’s cache is not configurable. Actually MongoDb uses all the free spaces on the system automatically by way of memory mapped files.

25. How to do Transaction/locking in MongoDB?
MongoDB doesn’t use traditional locking or complex transaction with Rollback. MongoDB is designed to be light weighted, fast and predictable to its performance. It keeps transaction support simple to enhance performance.

26. Why 32 bit version of MongoDB are not preferred ?
Because MongoDB uses memory mapped files so when you run a 32-bit build of MongoDB, the total storage size of server is 2 GB. But when you run a 64-bit build of MongoDB, this provides virtually unlimited storage size. So 64-bit is preferred over 32-bit.

27. Is it possible to remove old files in the moveChunk directory?
Yes, These files can be deleted once the operations are done because these files are made as backups during normal shard balancing operation. This is a manual cleanup process and necessary to free up space.

28. What will have to do if a shard is down or slow and you do a query?
If a shard is down and you even do query then your query will be returned with an error unless you set a partial query option. But if a shard is slow them Mongos will wait for them till response.

29. Explain the covered query in MongoDB.
A query is called covered query if satisfies the following two conditions:

  • The fields used in the query are part of an index used in the query.
  • The fields returned in the results are in the same index.

30. What is the importance of covered query?
Covered query makes the execution of the query faster because indexes are stored in RAM or sequentially located on disk. It makes the execution of the query faster.

Covered query makes the fields are covered in the index itself, MongoDB can match the query condition as well as return the result fields using the same index without looking inside the documents.

31. What is sharding in MongoDB?
In MongoDB, Sharding is a procedure of storing data records across multiple machines. It is a MongoDB approach to meet the demands of data growth. It creates horizontal partition of data in a database or search engine. Each partition is referred as shard or database shard.

32. What is replica set in MongoDB?
A replica can be specified as a group of mongo instances that host the same data set. In a replica set, one node is primary, and another is secondary. All data is replicated from primary to secondary nodes.

33. What is primary and secondary replica set in MongoDB?
In MongoDB, primary nodes are the node that can accept write. These are also known as master nodes. The replication in MongoDB is single master so, only one node can accept write operations at a time.

Secondary nodes are known as slave nodes. These are read only nodes that replicate from the primary.

34. By default, which replica sets are used to write data?
By default, MongoDB writes data only to the primary replica set.

35. What is CRUD in MongoDB?
MongoDB supports following CRUD operations:

  • Create
  • Read
  • Update
  • Delete

36. In which format MongoDB represents document structure?
MongoDB uses BSON to represent document structures.

37. What will happen when you remove a document from database in MongoDB? Does MongoDB remove it from disk?
Yes. If you remove a document from database, MongoDB will remove it from disk too.

38. Why are MongoDB data files large in size?
MongoDB doesn’t follow file system fragmentation and pre allocates data files to reserve space while setting up the server. That’s why MongoDB data files are large in size.

39. What is a storage engine in MongoDB?
A storage engine is the part of a database that is used to manage how data is stored on disk.

For example: one storage engine might offer better performance for read-heavy workloads, and another might support a higher-throughput for write operations.

40. Which are the storage engines used by MongoDB?
MMAPv1 and WiredTiger are two storage engine used by MongoDB.

41. What is the usage of profiler in MongoDB?
A database profiler is used to collect data about MongoDB write operations, cursors, database commands on a running mongod instance. You can enable profiling on a per-database or per-instance basis.

The database profiler writes all the data it collects to the system. profile collection, which is a capped collection.

42. Is it possible to configure the cache size for MMAPv1 in MongoDB?
No. it is not possible to configure the cache size for MMAPv1 because MMAPv1 does not allow configuring the cache size.

43. How to configure the cache size for WiredTiger in MongoDB?
For the WiredTiger storage engine, you can specify the maximum size of the cache that WiredTiger will use for all data. This can be done using storage.wiredTiger.engineConfig.cacheSizeGB option.

44. How does MongoDB provide concurrency?
MongoDB uses reader-writer locks for concurrency. Reader-writer locks allow concurrent readers shared access to a resource, such as a database or collection, but give exclusive access to a single write operation.

45. What is the difference between MongoDB and Redis database?
Difference between MongoDB and Redis:

  • Redis is faster than MongoDB.
  • Redis has a key-value storage whereas MongoDB has a document type storage.
  • Redis is hard to code but MongoDB is easy.

46. What is the difference between MongoDB and CouchDB?
Difference between MongoDB and CouchDB:

  • MongoDB is faster than CouchDB while CouchDB is safer than MongoDB.
  • Triggers are not available in MongoDB while triggers are available in CouchDB.
  • MongoDB serializes JSON data to BSON while CouchDB doesn’t store data in JSON format.

47. What is the difference between MongoDB and Cassandra?
Difference between MongoDB and Cassandra:

  • MongoDB is cross-platform document-oriented database system while Cassandra is high performance distributed database system.
  • MongoDB is written in C++ while Cassandra is written in Java.
  • MongoDB is easy to administer in the case of failure while Cassandra provides high availability with no single point of failure.

48. Is there any need to create database command in MongoDB?
You don’t need to create a database manually in MongoDB because it creates automaically when you save the value into the defined collection at first time.

49. What do you understand by NoSQL databases? Is MongoDB a NoSQL database? explain.
At the present time, the internet is loaded with big data, big users, big complexity etc. and also becoming more complex day by day. NoSQL is answer of all these problems, It is not a traditional database management system, not even a relational database management system (RDBMS). NoSQL stands for “Not Only SQL”. NoSQL is a type of database that can handle and sort all type of unstructured, messy and complicated data. It is just a new way to think about the database.

Yes. MongoDB is a NoSQL database.

MongoDB Questions and Answers Pdf Download

300+ [LATEST] Mongodb Interview Questions and Answers

Q1. How Can You Isolate Your Cursors From Intervening With The Write Operations?

You can use the snapshot() method on a cursor to isolate the operation for a very specific case. snapshot() traverses the index on the _id field and guarantees that the query will return each document no more than once.

Q2. Which Are The Two Storage Engines Used By Mongodb?

MongoDB uses MMAPv1 and WiredTiger.

Q3. Why Are Mongodb Data Files Large In Size?

MongoDB preallocate data files to reserve space and avoid file system fragmentation when you setup the server.

Q4. Mention The Command To Check Whether You Are On The Master Server Or Not.?

db.isMaster()

Q5. Mongodb Uses Bson To Represent Document Structures. True Or False?

True

Q6. Why Mongodb Is Not Preferred Over A 32-bit System?

When running a 32-bit build of MongoDB, the total storage size for the server, including data and indexes, is 2 gigabytes. For this reason, do not deploy MongoDB to production on 32-bit machines.
If you’re running a 64-bit build of MongoDB, there’s virtually no limit to storage size.

Q7. How Can Concurrency Affect Replica Sets Primary?

In replication, when MongoDB writes to a collection on the primary, MongoDB also writes to the primary’s oplog, which is a special collection in the local database. Therefore, MongoDB must lock both the collection’s database and the local database.

Q8. Compare Sql Databases And Mongodb At A High Level.?

SQL databases store data in form of tables, rows, columns and records. This data is stored in a pre-defined data model which is not very much flexible for today’s real-world highly growing applications. MongoDB in contrast uses a flexible structure which can be easily modified and extended.

Q9. Mention The Command To Insert A Document In A Database Called School And Collection Called Persons.?

use school;
db.persons.insert( { name: “kadhir”, dept: “CSE” } )

Q10. Does Mongodb Support Acid Traction Management And Locking Functionalities?

No. MongoDB does not support default multi-document ACID tractions. However, MongoDB provides atomic operation on a single document.

Q11. What Is Sharding In Mongodb? Explain.?

Sharding is a method for storing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.

Q12. How Does Journaling Work In Mongodb?

When running with journaling, MongoDB stores and applies write operations in memory and in the on-disk journal before the changes are present in the data files on disk. Writes to the journal are atomic, ensuring the consistency of the on-disk journal files. With journaling enabled, MongoDB creates a journal subdirectory within the directory defined by dbPath, which is /data/db by default.

Q13. What Is A Storage Engine In Mongodb?

A storage engine is the part of a database that is responsible for managing how data is stored on disk. For example, one storage engine might offer better performance for read-heavy workloads, and another might support a higher-throughput for write operations.

Q14. What Are Primary And Secondary Replica Sets?

Primary and master nodes are the nodes that can accept writes. MongoDB’s replication is ‘single-master:’ only one node can accept write operations at a time.

Secondary and slave nodes are read-only nodes that replicate from the primary.

Q15. Which Command Can Be Used To Provide Various Information On The Query Pl Used By A Mongodb Query?

The explain() command can be used for this information. The possible modes are: ‘queryPlanner’, ‘executionStats’, and ‘allPlExecution’.

Q16. How Can You Achieve Traction And Locking In Mongodb?

To achieve concepts of traction and locking in MongoDB, we can use the nesting of documents, also called embedded documents. MongoDB supports atomic operations within a single document.

Q17. Mention The Command To List All The Indexes On A Particular Collection.?

db.collection.getIndexes()

Q18. What Is A Namespace In Mongodb?

A Namespace is the concatenation of the database name and collection name. For e.g. school.students with school as the database and students as the collection.

Q19. Why Is A Covered Query Important?

Since all the fields are covered in the index itself, MongoDB can match the query condition as well as return the result fields using the same index without looking inside the documents. Since indexes are stored in RAM or sequentially located on disk, such access is a lot faster.

Q20. Can You Configure The Cache Size For Mmapv1? How?

No. MMAPv1 does not allow configuring the cache size.

Q21. What Is Aggregation In Mongodb?

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods and commands.

Q22. Does Mongodb Support Foreign Key Constraints?

No. MongoDB does not support such relationships.

Q23. How Can You Achieve Primary Key – Foreign Key Relationships In Mongodb?

By default MongoDB does not support such primary key – foreign key relationships. However, we can achieve this concept by embedding one document inside another. Foe e.g. an address document can be embedded inside customer document.

Q24. Can One Mongodb Operation Lock More Than One Databases? If Yes, How?

Yes. Operations like copyDatabase(), repairDatabase(), etc. can lock more than onne databases involved.

Q25. How Is Mongodb Better Than Other Sql Databases?

MongoDB allows a highly flexible and scalable document structure. For e.g. one data document in MongoDB can have five columns and the other one in the same collection can have ten columns. Also, MongoDB database are faster as compared to SQL databases due to efficient indexing and storage techniques.

Q26. What Are Nosql Databases? What Are The Different Types Of Nosql Databases?

A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in me other than the tabular relations used in relational databases (like SQL, Oracle, etc.).

Types of NoSQL databases:

  • Document Oriented
  • Key Value
  • Graph
  • Column Oriented

Q27. How Does Mongodb Provide Concurrency?

MongoDB uses reader-writer locks that allow concurrent readers shared access to a resource, such as a database or collection, but give exclusive access to a single write operation.

Q28. Does Mongodb Need A Lot Of Ram?

No. MongoDB can be run even on a small amount of RAM. MongoDB dynamically allocates and de-allocates RAM based on the requirements of other processes.

Q29. Which All Languages Can Be Used With Mongodb?

Currently, MonggoDB provides official driver support for C, C++, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go and Erlang. MongoDB can easily be used with any of these languages. There are some other community supported drivers too but the above mentioned ones are officially provided by MongoDB.

Q30. What Kind Of Nosql Database Mongodb Is?

MongoDB is a document oriented database. It stores data in the form of BSON structure based documents. These documents are stored in a collection.

Q31. What Are Indexes In Mongodb?

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

Q32. How Many Indexes Does Mongodb Create By Default For A New Collection?

By default, MongoDB created the _id collection for every collection.

Q33. What Is Gridfs?

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document.

Q34. What Is A Covered Query In Mongodb?

A covered query is the one in which:

fields used in the query are part of an index used in the query, and the fields returned in the results are in the same index.

Q35. When Should We Embed One Document Within Another In Mongodb?

You should consider embedding documents for:

  • ‘contains’ relationships between entities
  • One-to-many relationships
  • Performance reasons

Q36. Explain The Structure Of Objectid In Mongodb.?

ObjectID is a 12-byte BSON type with:

  • 4 bytes value representing seconds
  • 3 byte machine identifier
  • 2 byte process id
  • 3 byte counter

Q37. At What Interval Does Mongodb Write Updates To The Disk?

By default configuration, MongoDB writes updates to the disk every 60 seconds. However, this is configurable with the commitIntervalMs and syncPeriodSecs options.

Q38. Does Mongodb Provide A Facility To Do Text Searches? How?

Yes. MongoDB supports creating text indexes to support text search inside string content. This was a new feature which can introduced in version 2.6.

Q39. Which Are The Most Important Features Of Mongodb?

  • Flexible data model in form of documents
  • Agile and highly scalable database
  • Faster than traditional databases
  • Expressive query language

Q40. Can You Run Multiple Javascript Operations In A Single Mongod Instance?

Yes. The V8 JavaScript engine added in 2.4 allows multiple JavaScript operations to run at the same time.

Q41. If You Remove A Document From Database, Does Mongodb Remove It From Disk?

Yes. Removing a document from database removes it from disk too.

Q42. Can You Create An Index On An Array Field In Mongodb? If Yes, What Happens In This Case?

Yes. An array field can be indexed in MongoDB. In this case, MongoDB would index each value of the array.

Q43. Compare Mongodb And Couchdb At High Level.?

Although both of these databases are document oriented, MongoDB is a better choice for applications which need dynamic queries and good performance on a very big database. On the other side, CouchDB is better used for applications with occasionally changing queries and pre-defined queries.

Q44. What Is Replication In Mongodb?

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions.

Q45. By Default, Mongodb Writes And Reads Data From Both Primary And Secondary Replica Sets. True Or False.

False. MongoDB writes data only to the primary replica set.

Q46. What Is The Role Of A Profiler In Mongodb? Where Does The Writes All The Data?

The database profiler collects fine grained data about MongoDB write operations, cursors, database commands on a running mongod instance. You can enable profiling on a per-database or per-instance basis.

The database profiler writes all the data it collects to the system.profile collection, which is a capped collection.

Q47. Does Mongodb Pushes The Writes To Disk Immediately Or Lazily?

MongoDB pushes the data to disk lazily. It updates the immediately written to the journal but writing the data from journal to disk happens lazily.

Q48. What Happens If An Index Does Not Fit Into Ram?

If the indexes do not fit into RAM, MongoDB reads data from disk which is relatively very much slower than reading from RAM.