DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Top 100 MongoDB Interview Questions and Answers

MongoDB Comprehensive Answers

πŸ”° Basic MongoDB Questions

  1. What is MongoDB?

    • MongoDB is a NoSQL, document-oriented database that provides high performance, high availability, and easy scalability. It stores data in flexible, JSON-like documents.
  2. How is MongoDB different from RDBMS?

    • MongoDB is schema-less, document-oriented, and horizontally scalable, while RDBMS is table-based, schema-enforced, and typically scaled vertically. MongoDB also doesn't support joins like RDBMS.
  3. What is a document in MongoDB?

    • A document is a set of key-value pairs (like JSON) that is the basic unit of data in MongoDB. Documents are stored in BSON format (binary JSON).
  4. What is a collection?

    • A collection is a group of MongoDB documents, similar to a table in RDBMS. Collections don't enforce a schema.
  5. What is a database in MongoDB?

    • A database is a physical container for collections. A single MongoDB server typically has multiple databases.
  6. What are the advantages of using MongoDB?

    • Flexible schema, horizontal scalability, high performance, rich query language, automatic sharding, replication, and support for multiple storage engines.
  7. What data types are supported in MongoDB?

    • String, Integer, Boolean, Double, Decimal128, ObjectId, Date, Timestamp, Array, Object (embedded document), Null, Binary data, Regular expression, JavaScript code, etc.
  8. What is BSON?

    • BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents that MongoDB uses to store documents and make remote procedure calls.
  9. What are indexes in MongoDB?

    • Indexes are special data structures that store a small portion of the collection's data in an easy-to-traverse form, improving query performance.
  10. What is a primary key in MongoDB?

    • A primary key is a unique identifier for a document in a collection. In MongoDB, the _id field serves as the primary key.
  11. What is the default primary key in MongoDB?

    • The default primary key is the _id field, which is automatically created if not specified, containing an ObjectId.
  12. How do you create a database in MongoDB?

    use databaseName
    // The database is created when you first store data in it
    
  13. How do you create a collection?

    db.createCollection("collectionName")
    // Or implicitly by inserting a document
    db.collectionName.insert({...})
    
  14. How do you insert a document?

    db.collection.insertOne({name: "John", age: 30})
    // or
    db.collection.insertMany([{...}, {...}])
    
  15. How do you read documents?

    db.collection.find()
    db.collection.findOne({age: {$gt: 25}})
    
  16. How do you update documents?

    db.collection.updateOne({name: "John"}, {$set: {age: 31}})
    db.collection.updateMany({status: "active"}, {$inc: {views: 1}})
    
  17. How do you delete a document?

    db.collection.deleteOne({name: "John"})
    db.collection.deleteMany({status: "inactive"})
    
  18. How do you drop a collection?

    db.collection.drop()
    
  19. How do you drop a database?

    db.dropDatabase()
    
  20. What is ObjectId?

    • A 12-byte unique identifier typically used as the _id field in documents. It consists of:
      • 4-byte timestamp
      • 5-byte random value
      • 3-byte incrementing counter

πŸ”„ CRUD Operations

  1. How do you insert multiple documents at once?

    db.collection.insertMany([
      {name: "Alice", age: 25},
      {name: "Bob", age: 30}
    ])
    
  2. What does the find() method return?

    • Returns a cursor to the documents that match the query criteria. You can iterate through the cursor to access the documents.
  3. How can you filter documents in find()?

    db.collection.find({age: {$gt: 25}, status: "active"})
    
  4. What is the difference between find() and findOne()?

    • find() returns a cursor to multiple documents, while findOne() returns a single document or null.
  5. How do you update a nested field in a document?

    db.collection.updateOne(
      {_id: 1},
      {$set: {"address.city": "New York"}}
    )
    
  6. What is $set, $unset, $inc, $push, $pull?

    • $set: Sets the value of a field
    • $unset: Removes a field
    • $inc: Increments a field's value
    • $push: Adds an item to an array
    • $pull: Removes items from an array that match a condition
  7. How to replace an entire document?

    db.collection.replaceOne(
      {_id: 123},
      {name: "New Name", age: 40}
    )
    
  8. What does upsert mean?

    • An operation that updates a document if it exists, or inserts a new document if it doesn't exist.
    db.collection.updateOne(
      {name: "John"},
      {$set: {age: 35}},
      {upsert: true}
    )
    
  9. How do you delete multiple documents?

    db.collection.deleteMany({status: "inactive"})
    
  10. How do you use limit, skip, and sort?

    db.collection.find()
      .sort({age: -1})  // descending
      .skip(10)         // skip first 10
      .limit(5)         // return 5
    

🧠 Aggregation Framework

  1. What is aggregation in MongoDB?

    • A framework for performing data processing operations on documents, similar to SQL's GROUP BY and JOIN operations.
  2. How is aggregation different from MapReduce?

    • Aggregation uses a pipeline model with stages, while MapReduce uses JavaScript functions. Aggregation is generally faster for most operations.
  3. What is the $match stage?

    • Filters documents to pass only those that match specified conditions to the next stage.
    {$match: {status: "A"}}
    
  4. What is the $group stage?

    • Groups documents by specified identifier and applies accumulator expressions.
    {$group: {_id: "$department", total: {$sum: "$salary"}}}
    
  5. What is the $project stage?

    • Reshapes each document by including, excluding, or adding fields.
    {$project: {name: 1, department: 1, _id: 0}}
    
  6. What does $sort do in aggregation?

    • Sorts all input documents and returns them in sorted order.
    {$sort: {age: -1, name: 1}}
    
  7. What is $lookup used for?

    • Performs a left outer join to another collection.
    {
      $lookup: {
        from: "orders",
        localField: "user_id",
        foreignField: "_id",
        as: "user_orders"
      }
    }
    
  8. Explain $unwind.

    • Deconstructs an array field to output a document for each element.
    {$unwind: "$tags"}
    
  9. What is $facet used for?

    • Processes multiple aggregation pipelines within a single stage.
    {
      $facet: {
        "price": [{$group: {_id: null, avg: {$avg: "$price"}}}],
        "count": [{$count: "total"}]
      }
    }
    
  10. How do you calculate total or average using aggregation?

    db.sales.aggregate([
      {
        $group: {
          _id: null,
          total: {$sum: "$amount"},
          average: {$avg: "$amount"}
        }
      }
    ])
    

πŸ—ƒοΈ Data Modeling

  1. When to use embedding vs. referencing?

    • Embed when:
      • Data has a "contains" relationship
      • One-to-many where the "many" are viewed with the parent
      • Data doesn't change frequently
    • Reference when:
      • Data has many-to-many relationships
      • Embedded data would result in duplication
      • Child documents are large
  2. What is normalization and denormalization in MongoDB?

    • Normalization: Splitting data into multiple collections with references (like RDBMS)
    • Denormalization: Embedding related data in a single document (common in MongoDB)
  3. What are schema-less documents?

    • Documents in the same collection don't need to have the same fields or structure. However, in practice, applications usually enforce some schema.
  4. What is a capped collection?

    • Fixed-size collections that maintain insertion order. Once the size is reached, oldest documents are overwritten.
  5. What is a TTL index?

    • A special index that automatically removes documents after a specified time period.
    db.logs.createIndex({createdAt: 1}, {expireAfterSeconds: 3600})
    
  6. What is a covered query?

    • A query where all fields in the query are part of an index, and all fields returned are in the same index, so MongoDB can fulfill the query without examining documents.
  7. How do you model a one-to-many relationship?

    • For small N: Embed the many side as an array
    • For large N: Use references (array of ObjectIds)
  8. How do you model a many-to-many relationship?

    • Use an array of references in one or both collections:
    // Books collection
    {_id: 1, title: "Book A", authors: [101, 102]}
    
    // Authors collection
    {_id: 101, name: "Author X", books: [1, 2]}
    
  9. What are the benefits of embedding?

    • Improved read performance (single read operation)
    • Atomic writes for the entire document
    • No joins needed
  10. What are the trade-offs of referencing?

    • Additional queries needed to resolve references
    • More complex application code
    • Potential consistency issues

βš™οΈ Indexing and Performance

  1. What types of indexes does MongoDB support?

    • Single field, Compound, Multikey (array), Text, Geospatial, Hashed, Wildcard, etc.
  2. What is a compound index?

    • An index on multiple fields.
    db.users.createIndex({lastName: 1, firstName: 1})
    
  3. What is a hashed index?

    • An index that hashes the field value for equality queries (used mainly for sharding).
    db.users.createIndex({username: "hashed"})
    
  4. What is a text index?

    • Supports text search queries on string content.
    db.articles.createIndex({content: "text"})
    
  5. How to create an index?

    db.collection.createIndex({field: 1}) // 1 for ascending, -1 for descending
    
  6. How to view all indexes on a collection?

    db.collection.getIndexes()
    
  7. How does indexing impact write performance?

    • Each index adds overhead to write operations since MongoDB must update all indexes when data changes.
  8. What is an index cardinality?

    • The uniqueness of values in the indexed field. High cardinality means many unique values (good for selectivity).
  9. How do you use explain plans?

    db.collection.find({...}).explain("executionStats")
    
  10. What does explain() show?

    • Query execution plan, indexes used, execution time, documents examined, etc.

πŸ”’ Security and Authentication

  1. What are the different levels of authentication in MongoDB?

    • SCRAM (default), x.509 certificates, LDAP, Kerberos
  2. What is role-based access control?

    • Users are assigned roles that define their privileges (read, readWrite, dbAdmin, etc.)
  3. What is MongoDB's default port?

    • 27017
  4. How do you enable access control?

    • Start mongod with --auth option or set security.authorization in config file
  5. How do you create a user in MongoDB?

    use admin
    db.createUser({
      user: "admin",
      pwd: "password",
      roles: ["root"]
    })
    
  6. How to encrypt data in MongoDB?

    • Field-level encryption (Client-Side Field Level Encryption)
    • Encryption at rest (Enterprise feature)
    • TLS/SSL for network encryption
  7. What is SCRAM authentication?

    • Salted Challenge Response Authentication Mechanism (default authentication mechanism)
  8. How do you secure a MongoDB deployment?

    • Enable authentication
    • Configure network encryption (TLS/SSL)
    • Set up firewalls
    • Regularly update MongoDB
    • Follow principle of least privilege for users
  9. What is IP whitelisting in MongoDB?

    • Restricting access to MongoDB from specific IP addresses only.
  10. What is MongoDB Atlas and how is it secured?

    • MongoDB's fully-managed cloud database service, secured with:
      • Network isolation (VPC peering)
      • Encryption at rest and in transit
      • IP whitelisting
      • Auditing
      • Automated patching

☁️ Replication and Sharding

  1. What is replication in MongoDB?

    • Maintaining multiple copies of data across different servers for high availability.
  2. What is a replica set?

    • A group of mongod processes that maintain the same data set, typically with one primary and multiple secondaries.
  3. How does failover work in MongoDB?

    • If the primary fails, an election occurs among secondaries to select a new primary.
  4. What are the components of a replica set?

    • Primary (accepts all writes)
    • Secondaries (replicate primary's oplog)
    • Arbiter (votes in elections but doesn't hold data)
  5. What is an arbiter node?

    • A lightweight mongod instance that votes in elections but doesn't store data (used to break ties).
  6. What is sharding?

    • Distributing data across multiple machines to support horizontal scaling.
  7. What is a shard key?

    • The field used to partition data across shards (must be chosen carefully).
  8. How is data distributed in a sharded cluster?

    • Data is partitioned into chunks based on the shard key range and distributed across shards.
  9. When should you use sharding?

    • When your data set exceeds the storage capacity of a single server
    • When your active working set exceeds RAM
    • When your write load exceeds a single server's capacity
  10. What are chunk migrations?

    • The process of moving chunks (ranges of shard key values) between shards to balance data distribution.

πŸ§ͺ Transactions & Consistency

  1. What are multi-document transactions?

    • Operations that allow you to perform reads and writes across multiple documents in one or more collections with ACID guarantees.
  2. Are MongoDB transactions ACID-compliant?

    • Yes, since MongoDB 4.0 for replica sets and 4.2 for sharded clusters.
  3. How do you start and commit a transaction?

    const session = db.getMongo().startSession();
    session.startTransaction();
    try {
      db.collection1.insertOne({...}, {session});
      db.collection2.updateOne({...}, {...}, {session});
      session.commitTransaction();
    } catch (error) {
      session.abortTransaction();
    }
    
  4. What are the limitations of MongoDB transactions?

    • Maximum lifetime of 60 seconds
    • Performance overhead
    • Some operations cannot be used in transactions (like creating collections)
  5. What is write concern?

    • The level of acknowledgment requested from MongoDB for write operations.
    db.collection.insertOne(
      {...},
      {writeConcern: {w: "majority", wtimeout: 5000}}
    )
    
  6. What is read concern?

    • The level of isolation for read operations.
    db.collection.find().readConcern("majority")
    
  7. What is journaling in MongoDB?

    • A write-ahead log that ensures data durability in case of a crash.
  8. How does MongoDB handle concurrency?

    • Uses document-level locking (since MongoDB 3.0) allowing concurrent operations on different documents.

πŸ“¦ Tools & Ecosystem

  1. What is MongoDB Compass?

    • The official GUI for MongoDB with schema visualization, query building, and performance analysis.
  2. What is MongoDB Atlas?

    • MongoDB's fully-managed cloud database service (DBaaS).
  3. What is Mongoose?

    • An ODM (Object Document Mapper) library for MongoDB and Node.js that provides schema validation and other features.
  4. How do you connect to MongoDB using Mongoose?

    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/mydb', {useNewUrlParser: true});
    
  5. What is the MongoDB shell?

    • An interactive JavaScript interface to MongoDB for administration and data manipulation.
  6. What is mongodump and mongorestore?

    • Utilities for backing up (mongodump) and restoring (mongorestore) MongoDB databases.
  7. What is MongoDB Ops Manager?

    • A management platform for MongoDB deployments (monitoring, backup, automation).
  8. What is GridFS in MongoDB?

    • A specification for storing and retrieving large files (greater than 16MB) by splitting them into chunks.
  9. What is a change stream?

    • A real-time API that allows applications to subscribe to all data changes in MongoDB.
    const changeStream = db.collection.watch();
    changeStream.on('change', (change) => { ... });
    
  10. What is the difference between MongoDB and Firebase?

    • MongoDB is a general-purpose NoSQL database, while Firebase is a BaaS (Backend as a Service) with real-time capabilities and additional services.
  11. What is MongoDB Realm?

    • A mobile database and synchronization service (formerly Realm) now integrated with MongoDB.
  12. How do you monitor MongoDB performance?

    • Using tools like:
      • MongoDB Atlas monitoring
      • Ops Manager/Cloud Manager
      • mongostat and mongotop commands
      • Database Profiler
      • explain() for query analysis

Top comments (1)

Collapse
 
stevsharp profile image
Spyros Ponaris

Thanks for sharing , you highlighted some great features of MongoDB!