MongoDB is a cross-platform, open-source, document-oriented NoSQL database. It uses JSON-like documents with dynamic schemas, enabling high scalability and flexibility. It provides features like automatic sharding, replication, and index support, and has a powerful query language with support for aggregation and real-time analytics. It’s used for modern web and mobile applications, IoT, and big data processing.
- What is MongoDB and what is it used for? MongoDB is a NoSQL document-oriented database that stores data in flexible, JSON-like documents. It is used for handling large volumes of structured, semi-structured, and unstructured data in a highly scalable manner.
- What is a document in MongoDB? A document is a basic unit of data in MongoDB, which is a JSON-like data structure that stores fields and values.
- What is a collection in MongoDB? A collection is a grouping of MongoDB documents that are stored in the same place and have a similar structure.
- What is the difference between MongoDB and SQL databases? MongoDB is a NoSQL database that stores data in flexible, JSON-like documents, while SQL databases are relational databases that store data in tables with fixed schemas.
- What is sharding in MongoDB? Sharding is the process of dividing a large MongoDB database into smaller, more manageable parts called shards. This enables MongoDB to handle large amounts of data and scale horizontally.
- What is replication in MongoDB? Replication is the process of synchronizing data across multiple MongoDB servers. This provides redundancy and increases availability in case one server goes down.
- What is the syntax for inserting a document in MongoDB? The syntax for inserting a document in MongoDB is as follows:
db.collection.insertOne({field1: value1, field2: value2, …})
- What is the syntax for updating a document in MongoDB? The syntax for updating a document in MongoDB is as follows:
db.collection.updateOne({filter}, {$set: {field1: value1, field2: value2, …}})
- What is the syntax for deleting a document in MongoDB? The syntax for deleting a document in MongoDB is as follows:
db.collection.deleteOne({filter})
- How do you create an index in MongoDB? You can create an index in MongoDB using the createIndex method, as follows:
db.collection.createIndex({field: 1})
- What is the difference between a single-field index and a compound index in MongoDB? A single-field index indexes a single field in a collection, while a compound index indexes multiple fields in a collection.
- How do you drop an index in MongoDB? You can drop an index in MongoDB using the dropIndex method, as follows:
db.collection.dropIndex({field: 1})
- What is the difference between a capped collection and a regular collection in MongoDB? A capped collection is a fixed-size collection that automatically overwrites old data when the collection becomes full, while a regular collection has no size limit and does not automatically overwrite old data.
- What is the difference between a read operation and a write operation in MongoDB? A read operation retrieves data from the database, while a write operation modifies or adds data to the database.
- What is the aggregation framework in MongoDB? The aggregation framework is a set of MongoDB operations that perform data processing and analysis on a collection.
- What is the $group operator in MongoDB?
The
$group
operator in MongoDB groups documents by a specified field and performs aggregate functions on the grouped data, such as sum, average, or count. - What is the $match operator in MongoDB?
The
$match
operator in MongoDB filters documents in a collection based on a specified condition. - What is the $project operator in MongoDB?
The
$project
operator in MongoDB modifies the output of an aggregation operation by selecting specific fields and transforming data. - What is the $lookup operator in MongoDB?
The
$lookup
operator in MongoDB performs a left outer join between two collections - What is the difference between a left outer join and an inner join in MongoDB? A left outer join returns all documents from the left collection and matching documents from the right collection, while an inner join only returns matching documents from both collections.
- What is the difference between $push and $addToSet in MongoDB?
The
$push
operator in MongoDB adds an element to an array field in a document, even if it already exists, while the$addToSet
operator only adds an element to an array field if it does not already exist. - What is the TTL index in MongoDB? The TTL index in MongoDB is a special type of index that automatically deletes documents from a collection after a specified amount of time has elapsed.
- What is the difference between a capped collection and a TTL collection in MongoDB? A capped collection is a fixed-size collection that automatically overwrites old data when the collection becomes full, while a TTL collection is a collection that automatically deletes documents after a specified amount of time has elapsed.
- What is the $unwind operator in MongoDB?
The
$unwind
operator in MongoDB deconstructs an array field in a document and outputs one document for each element in the array. - What is the difference between a standalone MongoDB deployment and a replica set deployment? A standalone MongoDB deployment consists of a single MongoDB instance, while a replica set deployment consists of multiple MongoDB instances that work together to provide high availability and data redundancy.
- What is the primary node in a MongoDB replica set? The primary node in a MongoDB replica set is the node that accepts all write operations and is responsible for replicating data to secondary nodes.
- What is the purpose of the oplog in MongoDB? The oplog in MongoDB is a collection that stores a record of all write operations that have occurred in a replica set. This allows secondary nodes to catch up with the primary node and keep their data in sync.
- What is a gridFS in MongoDB? GridFS is a file storage system in MongoDB that allows you to store and retrieve files that exceed the BSON document size limit of 16MB.
- What is the difference between the MongoDB shell and a MongoDB driver? The MongoDB shell is a command-line interface that allows you to interact with MongoDB using JavaScript commands, while a MongoDB driver is a software library that allows you to interact with MongoDB using programming languages such as Python, Java, or Node.js.