interview-questions
  1. mongo-db-interview-questions

Mongo DB Interview Questions & Answers


Basic Questions:

  1. What is MongoDB?

    • MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents.
  2. Explain the key features of MongoDB.

    • NoSQL, document-oriented, schema-less, supports dynamic queries, and easy horizontal scaling.
  3. What is BSON?

    • BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used in MongoDB.
  4. How is MongoDB different from relational databases?

    • MongoDB is schema-less, uses JSON-like documents, and does not require a fixed schema. It supports flexible, dynamic data models.
  5. What is a NoSQL database?

    • NoSQL databases are non-relational databases designed to handle large amounts of unstructured or semi-structured data.

MongoDB Basics:

  1. How do you insert a document in MongoDB?

    db.collection_name.insert({ key: "value", ... });
    
  2. Explain the structure of a MongoDB document.

    • Documents are key-value pairs, similar to JSON objects, and can contain nested documents or arrays.
  3. What is a collection in MongoDB?

    • A collection is a group of MongoDB documents, similar to a table in a relational database.
  4. How can you query documents in MongoDB?

    db.collection_name.find({ key: "value" });
    
  5. Explain the ObjectId in MongoDB.

    • ObjectId is a 12-byte identifier typically employed as the primary key for documents.

Indexing:

  1. What is indexing in MongoDB?

    • Indexing is the process of creating an index on a field in a MongoDB collection to improve query performance.
  2. How can you create an index in MongoDB?

    db.collection_name.createIndex({ key: 1 });
    
  3. What types of indexes are available in MongoDB?

    • Single field index, compound index, multi-key index, and text index.

Aggregation Framework:

  1. Explain the Aggregation Framework in MongoDB.

    • The Aggregation Framework is a set of operators that process data records and return computed results.
  2. Give an example of using the Aggregation Framework.

    db.collection_name.aggregate([
      { $match: { key: "value" } },
      { $group: { _id: "$grouping_key", count: { $sum: 1 } } }
    ]);
    

Schema Design:

  1. How do you design schemas in MongoDB?

    • MongoDB allows flexible schema design, where documents in a collection can have different fields.
  2. When would you embed a document in another document?

    • Embed documents when the relationship between them is one-to-one or one-to-few.
  3. When would you use database references (DBRefs) in MongoDB?

    • Use DBRefs when representing relationships with a large number of documents or in a many-to-many relationship.

CRUD Operations:

  1. Explain the concept of upsert in MongoDB.

    • Upsert is a combination of update and insert. If a document matches the criteria, it's updated; if not, a new document is inserted.
  2. How do you update a document in MongoDB?

    db.collection_name.update({ key: "value" }, { $set: { new_key: "new_value" } });
    
  3. How can you remove a document in MongoDB?

    db.collection_name.remove({ key: "value" });
    

Transactions:

  1. Does MongoDB support transactions?

    • Yes, MongoDB introduced multi-document transactions in version 4.0.
  2. How do you start a transaction in MongoDB?

    session = db.getMongo().startSession();
    session.startTransaction();
    

Sharding:

  1. What is sharding in MongoDB?

    • Sharding is the process of distributing data across multiple machines to improve performance and scalability.
  2. How do you enable sharding in MongoDB?

    • Use the sh.enableSharding("database_name") command.

Performance Optimization:

  1. How can you optimize MongoDB for better performance?

    • Proper indexing, efficient queries, and appropriate shard key selection for sharded clusters.
  2. Explain the explain() method in MongoDB.

    • The explain() method provides information on the execution plan of a query.

Security:

  1. How do you secure MongoDB?

    • Set up authentication, configure access controls, enable encryption, and regularly update MongoDB.
  2. What is role-based access control in MongoDB?

    • Role-based access control (RBAC) in MongoDB assigns roles to users, specifying their permissions.

Replication:

  1. What is replication in MongoDB?

    • Replication is the process of synchronizing data across multiple servers to ensure availability and fault tolerance.
  2. How do you set up replication in MongoDB?

    • Use the rs.initiate() command on the primary server and add secondary servers with rs.add().

Backup and Restore:

  1. How can you perform a backup in MongoDB?

    • Use the mongodump command, like:
    mongodump --db database_name --out /path/to/backup_directory
    
  2. Explain the process of restoring a MongoDB backup.

    • Use the mongorestore command, like:
    mongorestore --db database_name /path/to/backup_directory
    

Map-Reduce:

  1. What is Map-Reduce in MongoDB?

    • Map-Reduce is a data processing paradigm used for large-scale data processing in MongoDB.
  2. When would you use Map-Reduce in MongoDB?

    • Use Map-Reduce for complex data processing tasks that cannot be achieved with standard queries or the Aggregation Framework.

Change Streams:

  1. What are Change Streams in MongoDB?
    • Change Streams allow applications to receive real-time notifications about changes in a MongoDB collection.

Text Search:

  1. Does MongoDB support full-text search?

    • Yes, MongoDB has a text search feature that allows for full-text search queries.
  2. How do you perform a text search in MongoDB?

    db.collection_name.find({ $text: { $search: "search_term" } });
    

GridFS:

  1. What is GridFS in MongoDB?

    • GridFS is a specification for storing and retrieving large files in MongoDB.
  2. How do you use GridFS to store files in MongoDB?

    • Use the mongofiles command-line tool or the GridFS API to store and retrieve files.

Geospatial Indexing:

  1. Does MongoDB support geospatial indexing?

    • Yes, MongoDB supports geospatial indexing for spatial queries.
  2. How do you create a geospatial index in MongoDB?

    db.collection_name.createIndex({ location_field: "2dsphere"
    

}); ```

Integration with Programming Languages:

  1. Which programming languages can be used with MongoDB?

    • MongoDB has official drivers for languages like Python, Java, Node.js, and many others.
  2. How can you interact with MongoDB using Python?

    • Use the pymongo library to connect, insert, query, and update MongoDB documents.

Aggregation Operators:

  1. What is the purpose of the $unwind operator in MongoDB?

    • The $unwind operator is used to deconstruct arrays in a document, creating separate documents for each array element.
  2. Explain the $project operator in MongoDB.

    • The $project operator is used to reshape documents, including selecting or renaming fields.

Server-Side JavaScript:

  1. Does MongoDB support server-side JavaScript?
    • Yes, MongoDB allows the execution of JavaScript code on the server using the eval command.

TTL Index:

  1. What is a TTL index in MongoDB?
    • A TTL (Time-To-Live) index is a special type of index that automatically deletes documents after a specified period.

Connection Pooling:

  1. What is connection pooling in MongoDB?
    • Connection pooling is a mechanism that maintains a pool of database connections to improve performance and reduce the overhead of opening and closing connections.

Miscellaneous:

  1. How can you check the version of MongoDB?
    • Use the mongod --version command.