Frequently Asked MongoDB Interview Questions
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. What is the role of profiler in MongoDB?
The profile shows the characteristics of performance of each operation against the database. You can also find Queries using the profiler.
7. How long does a replica set failover take?
It may take about 10-30 seconds for the declaration of the primary by other members and to elect a new primary. During this interval of time, the cluster is down for primary operations, that means strong and writes consistent reads. Conversely, at any time also during this window you can execute consistent queries to secondaries.
8. What is a replica set?
A replica set is a cluster of Mongo instances, that host the same data set. The replica set has one primary and one secondary node, all the data from primary to the secondary node replicates.
9. How does Journaling work in MongoDB?
In MongoDB, the journal applies and stores write operations in memory and also in the on-disk journal before the changes present in the data files on disk. By ensuring the reliability of the on-disk journal files, writes to the journals are atomic.
MongoDB generates a journal subdirectory inside the directory defined by DB path, when journaling is enabled.
10. What are alternatives to MongoDB?
The alternatives to MongoDB are CouchDB, HBase, Cassandra, Riak, Redis.
11. Why is a covered query important?
MongoDB can match the condition of a query and return the result fields by using the same index without looking into the documents. As the indexes are located sequentially on disk or stored in RAM, such access is very much faster.
12. What is Aggregation in MongoDB?
The Aggregation operation is used to process data records and return computed results. This operation can perform different type of actions on the grouped data in order to return a single result.
MongoDB uses three ways to perform aggregation: the map reduce function, the aggregation pipeline and a single purpose aggregation commands and methods.
13. What features make MongoDB best?
The following features make MongoDB best:
• Rich query language
• High performance
• Easy scalability
• Document-oriented
• High availability
14. How can concurrency affect replica sets primary?
When MongoDB writes to a collection on the primary in replication,it also writes to the oplog of the primary, which is a unique collection in the local database.
Thus, MongoDB should lock both the local database and the collection’s database.
15. Why MongoDB is not preferred over a 32-bit system?
If 32-bit build of MongoDB is running, the total storage size for data, server and indexes is 2 gigabytes. Therefore do not use MongoDB for production on 32-bit machines. There is no limit to storage size, if 64-bit build of MongoBD is running.
16. Why MongoDB data files are larger in size?
For reserving the space, the MongoDB pre assigns the data files and avoid the fragmentation of the file system, when you setup the server.
17. How can you achieve locking and transaction in MongoDB?
In order to achieve the locking and transaction in MongoDB, we can use the nesting of documents, also referred as embedded documents. MongoDB allows atomic operations inside a single document.
18. How does MongoDB provide a facility to do text searches?
MongoDB allows creating text indexes to support text search within string content. This is a new feature which was introduced in 2.6 version.
19. What points you must take into consideration while creating Schema in MongoDB?
The following point must be taken into consideration:
• The Schema must be designed according to user requirements.
• If you are using two objects at a time, combine objects into a single document, or else separate them.
• Perform joins during write operation, and not when it is on read.
• Optimize your schema for most frequently used cases.
• Perform complex aggregation in the schema.
20. What is the best feature you can use in MongoDB to perform safe backups?
By using the journaling feature of MongoDB, you can perform safe backups.
21. How you can do transaction/ locking in MongoDB?
As MongoDB is designed to be fast, lightweight and predictable in performance, it does not use complex transactions or locking with rollback. It is thought similar to MySQL auto commit model.
The performance can be improved, especially in those systems that may run across many servers, by keeping transaction support very simple.
22. What is a Master or primary?
It is nothing but a node/member which is currently the primary and can process all writes for the replica set. At a failover event in a replica set, a different member can become primary.
23. What is a Secondary or a slave?
It is also a node/member that applies operations from the current primary. This can be done by following the replication oplog. The replication from primary to secondary is asynchronous, though the secondary will try to stay as close as possible to current.
24. What are the two storage engines used by MongoDB?
The Wired Tiger and MMAPv1 are the two storage engines used by MongoDB.
25. Compare MongoDB with CouchDB and CouchBase.
The MongoDB and CouchDB are the best examples of open source NoSQL database. These both are Document-oriented databases. Apart from storing the documents, they don’t share much in common.
There are lots of differences between CouchDB and MongoDB such as implementation of data-model, replication methods, interface, object storage etc.
26. 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.
27. Which command can be used to provide various information on the query plans used by a MongoDB query?
The explain() command can be used for this information. The possible modes are: ‘queryPlanner’, ‘executionStats’, and ‘allPlansExecution’.
28. 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.
29. 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.
30. 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.
31. Why are MongoDB data files large in size?
MongoDB preallocates data files to reserve space and avoid file system fragmentation when you setup the server.
32. What is Replication in MongoDB? Explain.
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.
33. 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.
34. How can you achieve transaction and locking in MongoDB?
To achieve concepts of transaction and locking in MongoDB, we can use the nesting of documents, also called embedded documents. MongoDB supports atomic operations within a single document.
35. 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.
36. 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
37. How many indexes does MongoDB create by default for a new collection?
By default, MongoDB created the _id collection for every collection.
38. MongoDB uses BSON to represent document structures. True or False?
True
39. 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
40. 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.
41. _______________ is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.
a) DataFS
b) GridFS
c) ReadFS
d) None of the mentioned
Answer:b
42. Point out the wrong statement :
a) By default GridFS limits chunk size to 255k
b) GridFS is useful only for storing files that exceed 16MB
c) When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed
d) All of the mentioned
Answer:b
43. How many does collections GridFS use to store files ?
a) 1
b) 2
c) 3
d) 4
Answer:b
44. The default chunk size is changed from 256k to 255k in version :
a) 3.0
b) 2.4.9
c) 2.4.10
d) None of the mentioned
Answer:b
45. Point out the wrong statement :
a) Client libraries provide a convenient, injection free, process to build these objects
b) MongoDB represents queries as BSON objects
c) Field names in MongoDB’s query language have semantic meaning
d) All of the mentioend
Answer:d
46. Which of the collection in GridFS stores the binary chunks ?
a) chunks
b) files
c) data
d) log
Answer:a
47. ___________ is the _id is of the data type chosen for the original document in files collections.
a) files._id
b) file._id
c) fileobj._id
d) None of the mentioned
Answer:a
48. Which of the following checks and repairs errors and inconsistencies in data storage ?
a) repairDatabase
b) repairedDatabase
c) recoverDatabase
d) All of the mentioned
Answer:a
49. The files_id field contains the _id of the chunk’s __________ document.
a) parent
b) child
c) top
d) None of the mentioned
Answer:a
50. Which of the following field allows efficient retrieval of chunks ?
a) n
b) name
c) object
d) 2n
Answer:a