Top 50 interview questions and answers of mongodb

Introduction to mongodb

MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases, MongoDB makes use of collections and documents. Documents consist of key-value pairs which are the basic unit of data in MongoDB. Collections contain sets of documents and function which is the equivalent of relational database tables. MongoDB is a database which came into light around the mid-2000s.

Moving To Questions and answers:

1. What are the best features of MongoDB?

Answer:

2. Can journaling feature be used to perform safe hot backups?

Answer: yes

3. When using replication, can some members use journaling and others not?

Answer: yes

4. What is 32-bit nuances?

Answer: There is an extra memory mapped file activity with journaling. This will further constrain the limited db size of 32-bit builds. For now, journaling by default is disabled on 32-bit systems.

5. What is the role of profiler in MongoDB?

Answer: MongoDB includes a database profiler which shows performance characteristics of each operation against the database. With this profiler you can find queries (and write operations) which are slower than they should be and use this information for determining when an index is needed.

6. Will there be journal replay programs in case of incomplete entries (if there is a failure in the middle of one)?

Answer: Each journal (group) write is consistent and won’t be replayed during recovery unless it is complete.

7. What is a ‘namespace’?

Answer: MongoDB stores BSON objects in collections. The concatenation of the database name and the collection name (with a period in between) is called a ‘namespace’.

8. What is the role of profiler in MongoDB?

Answer: MongoDB includes a database profiler which shows performance characteristics of each operation against the database. With this profiler you can find queries (and write operations) which are slower than they should be and use this information for determining when an index is needed.

9. Are null values allowed?

Answer: Yes, but only for the members of an object. A null cannot be added to the database collection as it isn’t an object. But {}can be added.

10. How do I do transactions/locking?

Answer: MongoDB does not use traditional locking or complex transactions with rollback, as it is designed to be light weight, fast and predictable in its performance. It can be thought of how analogous is to the MySQL’s MyISAM autocommit model. By keeping transaction support extremely simple, performance is enhanced, especially in a system that may run across many servers.

11. How long does replica set failover take?

Answer: It may take 10-30 seconds for the primary to be declared down by the other members and a new primary to be elected. During this window of time, the cluster is down for primary operations i.e writes and strong consistent reads. However, eventually consistent queries may be executed to secondaries at any time (in slaveOk mode), including during this window.

12. What’s a Master or Primary?

Answer: This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.

13. What’s a Master or Primary?

Answer: This is a node/member which is currently the primary and processes all writes for the replica set. During a failover event in a replica set, a different member can become primary.

14. Is it required to call ‘getLastError’ to make a write durable?

Answer: No. If ‘getLastError’ (aka ‘Safe Mode’) is not called, the server does exactly behave the way as if it has been called. The ‘getLastError’ call simply allows one to get a 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.

15. What’s a Secondary or Slave?

Answer: 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).

16. How does Sharding work with replication?

Answer: Each Shard is a logical collection of partitioned data. The shard could consist of a single server or a cluster of replicas. Using a replica set for each Shard is highly recommended.

17. Should you start out with Sharded or with a Non-Sharded MongoDB environment?

Answer: We suggest starting with Non-Sharded for simplicity and quick startup, unless your initial data set will not fit on single servers. Upgrading to Sharded from Non-sharded is easy and seamless, so there is not a lot of advantage in setting up Sharding before your data set is large.

18. When will data be on more than one Shard?

Answer: MongoDB Sharding is range-based. So all the objects in a collection lie into a chunk. Only when there is more than 1 chunk there is an option for multiple Shards to get data. Right now, the default chunk size is 64mb, so you need at least 64mb for migration.

19. What happens when a Shard is down or slow when querying?

Answer: 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.

20. How do you see the connections used by Mongos?

Answer: The following command needs to be used: db._adminCommand(“connPoolStats”);

If a ‘moveChunk’ fails, is it necessary to cleanup the partially moved docs?

No, chunk moves are consistent and deterministic. The move will retry and when completed, the data will be only on the new Shard.

21. What are the disadvantages of MongoDB?

Answer:

  • A 32-bit edition has 2GB data limit. After that it will corrupt the entire DB, including the existing data. A 64-bit edition won’t suffer from this bug/feature.
  • Default installation of MongoDB has asynchronous and batch commits turned on. Meaning, it lies when asked to store something in DB and commits all changes in a batch at a later time in future. If there is a server crash or power failure, all those commits buffered in memory will be lost. This functionality can be disabled, but then it will perform as good as or worse than MySQL.
  • MongoDB is only ideal for implementing things like analytics/caching where impact of small data loss is negligible.
  • In MongoDB, it’s difficult to represent relationships between data so you end up doing that manually by creating another table to represent the relationship between rows in two or more tables.

22. What are Indexes in MongoDB?

Answer: 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.

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

Answer: By default, the_id collection is created for every collection by MongoDB.

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

Answer: yes

25. In which language MongoDB is written?

Answer: MongoDB is written and implemented in C++.

26. What language you can use with MongoDB?

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

27. What is the method to configure the cache size in MongoDB?

Answer: MongoDB’s cache is not configurable. Actually MongoDb uses all the free spaces on the system automatically by way of memory mapped files.

28. Why 32 bit version of MongoDB are not preferred ?

Answer: 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.

29. Explain the covered query in MongoDB.

Answer: 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 sharding in MongoDB?

Answer: 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.

31. What is CRUD in MongoDB?

Answer: MongoDB supports following CRUD operations:

  • Create
  • Read
  • Update
  • Delete

32. In which format MongoDB represents document structure?

Answer: MongoDB uses BSON to represent document structures.

33. Why are MongoDB data files large in size?

Answer: 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.

34. Which are the storage engines used by MongoDB?

Answer: MMAPv1 and WiredTiger are two storage engine used by MongoDB.

35. What is the usage of profiler in MongoDB?

Answer:

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.

36. Is it possible to configure the cache size for MMAPv1 in MongoDB?

Answer: No. it is not possible to configure the cache size for MMAPv1 because MMAPv1 does not allow configuring the cache size.

37. What is the difference between MongoDB and Redis database?

Answer: 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.

38. Is there any need to create database command in MongoDB?

Answer: 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.

39. How to connect MongoDB with PHP?

Answer: The following steps will help you to connect MongoDB using PHP:

  • Select the MongoDB database
  • Create a collection
  • Using the insert() method, insert a document in MongoDB
  • Using the find() method, find all the documents
  • Using the update() method, update the document in MongoDB
  • After running the file, you will get the following output:

40. What is the different storage engine used in MongoDB?

Answer: Three main storage engines supported by MongoDB are:

  • WiredTiger Storage Engine.
  • In-Memory Storage Engine.
  • MMAPv1 Storage Engine.

41. How is MongoDB better than other SQL information bases?

Answer: MongoDB permits an exceptionally flexible and versatile archive structure. For example, one piece of information in MongoDB can have five segments, and the other one in a similar assortment can have ten segments. Likewise, MongoDB information bases are quicker when contrasted with SQL data sets because of proficient ordering and capacity methods.

42. Analyze MongoDB and CouchDB at a significant level.

Answer: Albeit both of these data sets are record situated, MongoDB is a definitive decision for applications that need dynamic inquiries and excellent execution on a significant information base. On the opposite side, CouchDB is better utilized for applications with periodically changing inquiries and pre-characterized questions.

43. Does MongoDB need a great deal of RAM?

Answer: No. MongoDB can arrive behind schedule on a modest quantity of RAM. MongoDB powerfully apportions and de-assigns RAM dependent on the necessities of different cycles

44. What is the work of Journaling MongoDB?

Answer: When running with journaling, MongoDB stores and applies compose tasks in memory and the on-disk journal before the progressions are available in the information records on disk. Keeps in touch with the journal are nuclear, guaranteeing the consistency of the on-disk journal records. With journaling empowered, MongoDB makes a diary subdirectory inside the registry characterized by dbPath, which is/information/DB as a matter.

45. Explain the term Database.

Answer: Databases are the group of collections in MongoDB. It can host many databases, with each grouping collections together. Following are some of the reserved databases:

  • admin
  • local
  • config

46. Explain the term ‘MongoDB Charts.’

Answer: MongoDB Charts is an apparatus to make visual portrayals of your MongoDB information. Information perception is a vital segment to a reasonable comprehension of your information, featuring connections mongo factors and making it simple to perceive examples and patterns inside your dataset. MongoDB Charts makes imparting your information a clear interaction by giving underlying devices to share and work together on perceptions effortlessly.

Some of the critical features are:

Aggregation Functionality
MongoDB Charts gives worked in collection usefulness. Accumulation permits you to deal with your assortment of measurements and perform computations like mean and standard deviation to understand your information further.

Consistent Integration with MongoDB Atlas
Diagrams furnish a consistent combination with MongoDB Atlas. You can interface MongoDB Charts to Atlas projects and rapidly begin envisioning your Atlas group information.

Document Data Handling
Outlines handle record-based information, including implanted particles and clusters. Using a settled information structure allows you to structure your information as it best fits your application while holding unique representation abilities.

47. What is Oplog in MongoDB?

Answer: The oplog (operations log) is an extraordinary covered assortment that keeps a moving record of all activities that adjust the information put away in your data sets. MongoDB applies an information base procedure on the essential and afterward records the procedure on the essential’s oplog.

48. Difference between BSON files used in MongoDB and JSON files.

Answer:

SON (JavaScript Object Notation)— like XML, for instance—is an understandable standard utilized for information trade. JSON has gotten the most broadly utilized norm for information trade on the web. JSON upholds information types like Booleans, numbers, strings, and exhibits.

BSON, on the other hand, is the paired encoding that MongoDB uses to store its archives. It is like JSON, yet it stretches out JSON to help more information types, similar to Date. BSON reports, not at all like JSON records, are requested. BSON typically takes less space than JSON and is quicker to cross. BSON, since it is parallel, is likewise speedier to encode and decipher.

49. What are the limitations of the 32 bit MongoDB version?

Answer:

MongoDB utilizes memory-planned documents. When we run a 32-bit work of MongoDB, the total size of capacity for the server, including records and information, is 2 gigabytes. Because of this, don’t convey MongoDB to creation on 32-bit machines.

What’s more, in the 64-bit version of MongoDB, there’s practically no restriction to capacity size. For creation organizations, we firmly suggest 64-bit fabricates and working frameworks.

50. What are the alternatives of MongoDB?

Answer: Some of the best alternatives of MongoDB are:

  • RavenDB.
  • RethinkDB.
  • OrientDB.
  • CouchDB.
  • PostgreSQL.
  • Apache Cassandra.
  • IBM Cloudant.
Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x