Basic Questions:
What is MongoDB?
- MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents.
Explain the key features of MongoDB.
- NoSQL, document-oriented, schema-less, supports dynamic queries, and easy horizontal scaling.
What is BSON?
- BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents used in MongoDB.
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.
What is a NoSQL database?
- NoSQL databases are non-relational databases designed to handle large amounts of unstructured or semi-structured data.
MongoDB Basics:
How do you insert a document in MongoDB?
db.collection_name.insert({ key: "value", ... });
Explain the structure of a MongoDB document.
- Documents are key-value pairs, similar to JSON objects, and can contain nested documents or arrays.
What is a collection in MongoDB?
- A collection is a group of MongoDB documents, similar to a table in a relational database.
How can you query documents in MongoDB?
db.collection_name.find({ key: "value" });
Explain the ObjectId in MongoDB.
- ObjectId is a 12-byte identifier typically employed as the primary key for documents.
Indexing:
What is indexing in MongoDB?
- Indexing is the process of creating an index on a field in a MongoDB collection to improve query performance.
How can you create an index in MongoDB?
db.collection_name.createIndex({ key: 1 });
What types of indexes are available in MongoDB?
- Single field index, compound index, multi-key index, and text index.
Aggregation Framework:
Explain the Aggregation Framework in MongoDB.
- The Aggregation Framework is a set of operators that process data records and return computed results.
Give an example of using the Aggregation Framework.
db.collection_name.aggregate([ { $match: { key: "value" } }, { $group: { _id: "$grouping_key", count: { $sum: 1 } } } ]);
Schema Design:
How do you design schemas in MongoDB?
- MongoDB allows flexible schema design, where documents in a collection can have different fields.
When would you embed a document in another document?
- Embed documents when the relationship between them is one-to-one or one-to-few.
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:
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.
How do you update a document in MongoDB?
db.collection_name.update({ key: "value" }, { $set: { new_key: "new_value" } });
How can you remove a document in MongoDB?
db.collection_name.remove({ key: "value" });
Transactions:
Does MongoDB support transactions?
- Yes, MongoDB introduced multi-document transactions in version 4.0.
How do you start a transaction in MongoDB?
session = db.getMongo().startSession(); session.startTransaction();
Sharding:
What is sharding in MongoDB?
- Sharding is the process of distributing data across multiple machines to improve performance and scalability.
How do you enable sharding in MongoDB?
- Use the
sh.enableSharding("database_name")
command.
- Use the
Performance Optimization:
How can you optimize MongoDB for better performance?
- Proper indexing, efficient queries, and appropriate shard key selection for sharded clusters.
Explain the explain() method in MongoDB.
- The
explain()
method provides information on the execution plan of a query.
- The
Security:
How do you secure MongoDB?
- Set up authentication, configure access controls, enable encryption, and regularly update MongoDB.
What is role-based access control in MongoDB?
- Role-based access control (RBAC) in MongoDB assigns roles to users, specifying their permissions.
Replication:
What is replication in MongoDB?
- Replication is the process of synchronizing data across multiple servers to ensure availability and fault tolerance.
How do you set up replication in MongoDB?
- Use the
rs.initiate()
command on the primary server and add secondary servers withrs.add()
.
- Use the
Backup and Restore:
How can you perform a backup in MongoDB?
- Use the
mongodump
command, like:
mongodump --db database_name --out /path/to/backup_directory
- Use the
Explain the process of restoring a MongoDB backup.
- Use the
mongorestore
command, like:
mongorestore --db database_name /path/to/backup_directory
- Use the
Map-Reduce:
What is Map-Reduce in MongoDB?
- Map-Reduce is a data processing paradigm used for large-scale data processing in MongoDB.
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:
- What are Change Streams in MongoDB?
- Change Streams allow applications to receive real-time notifications about changes in a MongoDB collection.
Text Search:
Does MongoDB support full-text search?
- Yes, MongoDB has a text search feature that allows for full-text search queries.
How do you perform a text search in MongoDB?
db.collection_name.find({ $text: { $search: "search_term" } });
GridFS:
What is GridFS in MongoDB?
- GridFS is a specification for storing and retrieving large files in MongoDB.
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.
- Use the
Geospatial Indexing:
Does MongoDB support geospatial indexing?
- Yes, MongoDB supports geospatial indexing for spatial queries.
How do you create a geospatial index in MongoDB?
db.collection_name.createIndex({ location_field: "2dsphere"
}); ```
Integration with Programming Languages:
Which programming languages can be used with MongoDB?
- MongoDB has official drivers for languages like Python, Java, Node.js, and many others.
How can you interact with MongoDB using Python?
- Use the
pymongo
library to connect, insert, query, and update MongoDB documents.
- Use the
Aggregation Operators:
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.
- The
Explain the
$project
operator in MongoDB.- The
$project
operator is used to reshape documents, including selecting or renaming fields.
- The
Server-Side JavaScript:
- Does MongoDB support server-side JavaScript?
- Yes, MongoDB allows the execution of JavaScript code on the server using the
eval
command.
- Yes, MongoDB allows the execution of JavaScript code on the server using the
TTL Index:
- 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:
- 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:
- How can you check the version of MongoDB?
- Use the
mongod --version
command.
- Use the