MongoDB Interview Questions & Answers

  1. Question 1. What Are Nosql Databases? What Are The Different Types Of Nosql Databases?

    Answer :

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

    Types of NoSQL databases:

    • Document Oriented
    • Key Value
    • Graph
    • Column Oriented
  2. Question 2. What Kind Of Nosql Database Mongodb Is?

    Answer :

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

  3. Python Interview Questions

  4. Question 3. Which Are The Most Important Features Of Mongodb?

    Answer :

    • Flexible data model in form of documents
    • Agile and highly scalable database
    • Faster than traditional databases
    • Expressive query language
  5. Question 4. What Is A Namespace In Mongodb?

    Answer :

    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.

  6. Python Tutorial

  7. Question 5. Which All Languages Can Be Used With Mongodb?

    Answer :

    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.

  8. C & Data Structures Interview Questions

  9. Question 6. Compare Sql Databases And Mongodb At A High Level.?

    Answer :

    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.

  10. Question 7. How Is Mongodb Better Than Other Sql Databases?

    Answer :

    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.

  11. Hadoop Tutorial
    Hadoop Interview Questions

  12. Question 8. Compare Mongodb And Couchdb At High Level.?

    Answer :

    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.

  13. Question 9. Does Mongodb Support Foreign Key Constraints?

    Answer :

    No. MongoDB does not support such relationships.

  14. Node.js Interview Questions

  15. Question 10. Does Mongodb Support Acid Transaction Management And Locking Functionalities?

    Answer :

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

  16. Node.js Tutorial

  17. Question 11. How Can You Achieve Primary Key – Foreign Key Relationships In Mongodb?

    Answer :

    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.

  18. Scala Interview Questions

  19. Question 12. Does Mongodb Need A Lot Of Ram?

    Answer :

    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.

  20. Python Interview Questions

  21. Question 13. Does Mongodb Pushes The Writes To Disk Immediately Or Lazily?

    Answer :

    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.

  22. Scala Tutorial

  23. Question 14. Explain The Structure Of Objectid In Mongodb.?

    Answer :

    ObjectID is a 12-byte BSON type with:

    • 4 bytes value representing seconds
    • 3 byte machine identifier
    • 2 byte process id
    • 3 byte counter
  24. Question 15. Mongodb Uses Bson To Represent Document Structures. True Or False?

    Answer :

    True

  25. Socket Programming Interview Questions

  26. Question 16. If You Remove A Document From Database, Does Mongodb Remove It From Disk?

    Answer :

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

  27. JSON (JavaScript Object Notation) Tutorial

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

    Answer :

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

  29. NoSQL Interview Questions

  30. Question 18. What Are Indexes In Mongodb?

    Answer :

    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.

  31. C & Data Structures Interview Questions

  32. Question 19. How Many Indexes Does Mongodb Create By Default For A New Collection?

    Answer :

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

  33. HBase Tutorial

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

    Answer :

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

  35. JSON (JavaScript Object Notation) Interview Questions

  36. Question 21. What Is A Covered Query In Mongodb?

    Answer :

    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. Question 22. Why Is A Covered Query Important?

    Answer :

    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.

  38. Lucene Tutorial

  39. Question 23. Does Mongodb Provide A Facility To Do Text Searches? How?

    Answer :

    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.

  40. HBase Interview Questions

  41. Question 24. What Happens If An Index Does Not Fit Into Ram?

    Answer :

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

  42. Hadoop Interview Questions

  43. Question 25. Mention The Command To List All The Indexes On A Particular Collection.?

    Answer :

    db.collection.getIndexes()

  44. Question 26. At What Interval Does Mongodb Write Updates To The Disk?

    Answer :

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

  45. Solaris Administrator Interview Questions

  46. Question 27. How Can You Achieve Transaction And Locking In Mongodb?

    Answer :

    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.

  47. Node.js Interview Questions

  48. Question 28. What Is Aggregation In Mongodb?

    Answer :

    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.

  49. Question 29. What Is Sharding In Mongodb? Explain.?

    Answer :

    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.

  50. Question 30. What Is Replication In Mongodb?

    Answer :

    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.

  51. Question 31. What Are Primary And Secondary Replica Sets?

    Answer :

    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.

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

    Answer :

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

  53. Question 33. Why Are Mongodb Data Files Large In Size?

    Answer :

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

  54. Scala Interview Questions

  55. Question 34. When Should We Embed One Document Within Another In Mongodb?

    Answer :

    You should consider embedding documents for:

    • ‘contains’ relationships between entities
    • One-to-many relationships
    • Performance reasons
  56. Question 35. Why Mongodb Is Not Preferred Over A 32-bit System?

    Answer :

    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.

  57. Question 36. What Is A Storage Engine In Mongodb?

    Answer :

    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.

  58. Socket Programming Interview Questions

  59. Question 37. Which Are The Two Storage Engines Used By Mongodb?

    Answer :

    MongoDB uses MMAPv1 and WiredTiger.

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

    Answer :

    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.

  61. Question 39. How Does Journaling Work In Mongodb?

    Answer :

    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.

  62. Question 40. Mention The Command To Check Whether You Are On The Master Server Or Not.?

    Answer :

    db.isMaster()

  63. NoSQL Interview Questions

  64. Question 41. Can You Configure The Cache Size For Mmapv1? How?

    Answer :

    No. MMAPv1 does not allow configuring the cache size.

  65. Question 42. How Does Mongodb Provide Concurrency?

    Answer :

    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.

  66. JSON (JavaScript Object Notation) Interview Questions

  67. Question 43. How Can You Isolate Your Cursors From Intervening With The Write Operations?

    Answer :

    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.

  68. Question 44. Can One Mongodb Operation Lock More Than One Databases? If Yes, How?

    Answer :

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

  69. Question 45. How Can Concurrency Affect Replica Sets Primary?

    Answer :

    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.

  70. Question 46. What Is Gridfs?

    Answer :

    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.

  71. Question 47. Can You Run Multiple Javascript Operations In A Single Mongod Instance?

    Answer :

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

  72. Question 48. Which Command Can Be Used To Provide Various Information On The Query Plans Used By A Mongodb Query?

    Answer :

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