MongoDB is a popular NoSQL database that has gained immense popularity in recent years. It is an open-source, document-oriented database that stores data in JSON-like documents with dynamic schemas. MongoDB is designed to be scalable, flexible, and easy to use, making it a popular choice for modern web applications.
One of the key features of MongoDB is its ability to handle large amounts of unstructured data. Unlike traditional relational databases, MongoDB does not require a predefined schema, which means that data can be added or removed without the need for complex migrations. This makes it ideal for applications that require frequent updates or changes to the data model.
Another advantage of MongoDB is its ability to scale horizontally. This means that as the amount of data grows, additional servers can be added to the cluster to handle the load. This makes it easy to scale applications as they grow, without the need for complex sharding or partitioning.
MongoDB also offers a powerful query language that allows developers to retrieve data in a flexible and efficient manner. The query language supports a wide range of operators and functions, making it easy to filter, sort, and aggregate data.
Overall, MongoDB is a powerful and flexible database that is well-suited for modern web applications. Its ability to handle large amounts of unstructured data, scale horizontally, and provide a powerful query language make it a popular choice for developers around the world.
Cheat Sheet
This cheat sheet provides a quick reference guide for MongoDB commands and syntax.
Basic Commands
Command | Description |
---|---|
mongo | Start the MongoDB shell |
show dbs | Show all databases |
use <database> | Switch to a specific database |
show collections | Show all collections in the current database |
db.<collection>.find() | Show all documents in a collection |
db.<collection>.insertOne(<document>) | Insert a new document into a collection |
db.<collection>.updateOne(<filter>, <update>) | Update a document in a collection |
db.<collection>.deleteOne(<filter>) | Delete a document from a collection |
Querying Data
Command | Description |
---|---|
db.<collection>.find() | Show all documents in a collection |
db.<collection>.find(<filter>) | Show documents that match a filter |
db.<collection>.find(<filter>).pretty() | Show formatted documents that match a filter |
db.<collection>.findOne(<filter>) | Show the first document that matches a filter |
db.<collection>.count() | Count the number of documents in a collection |
db.<collection>.distinct(<field>) | Show distinct values for a field in a collection |
Filtering Data
Command | Description |
---|---|
{ <field>: <value> } | Filter documents where a field equals a value |
{ <field>: { $gt: <value> } } | Filter documents where a field is greater than a value |
{ <field>: { $lt: <value> } } | Filter documents where a field is less than a value |
{ <field>: { $gte: <value> } } | Filter documents where a field is greater than or equal to a value |
{ <field>: { $lte: <value> } } | Filter documents where a field is less than or equal to a value |
{ <field>: { $ne: <value> } } | Filter documents where a field is not equal to a value |
{ <field>: { $in: [<value1>, <value2>, ...] } } | Filter documents where a field is in a list of values |
{ <field>: { $nin: [<value1>, <value2>, ...] } } | Filter documents where a field is not in a list of values |
{ <field>: { $exists: true } } | Filter documents where a field exists |
{ <field>: { $exists: false } } | Filter documents where a field does not exist |
{ $and: [ <filter1>, <filter2>, ... ] } | Filter documents that match multiple conditions |
{ $or: [ <filter1>, <filter2>, ... ] } | Filter documents that match at least one condition |
{ $not: { <filter> } } | Filter documents that do not match a condition |
Updating Data
Command | Description |
---|---|
db.<collection>.updateOne(<filter>, <update>) | Update a document that matches a filter |
db.<collection>.updateOne(<filter>, { $set: { <field>: <value> } }) | Set a field to a value in a document that matches a filter |
db.<collection>.updateOne(<filter>, { $unset: { <field>: """" } }) | Remove a field from a document that matches a filter |
db.<collection>.updateOne(<filter>, { $inc: { <field>: <value> } }) | Increment a field by a value in a document that matches a filter |
db.<collection>.updateOne(<filter>, { $push: { <field>: <value> } }) | Add a value to an array field in a document that matches a filter |
db.<collection>.updateOne(<filter>, { $pull: { <field>: <value> } }) | Remove a value from an array field in a document that matches a filter |
Deleting Data
Command | Description |
---|---|
db.<collection>.deleteOne(<filter>) | Delete a document that matches a filter |
db.<collection>.deleteMany(<filter>) | Delete all documents that match a filter |
db.<collection>.drop() | Delete a collection |
Indexing Data
Command | Description |
---|---|
db.<collection>.createIndex(<field>) | Create an index on a field |
db.<collection>.createIndex({ <field>: 1 }) | Create an ascending index on a field |
db.<collection>.createIndex({ <field>: -1 }) | Create a descending index on a field |
db.<collection>.getIndexes() | Show all indexes on a collection |
db.<collection>.dropIndex(<index>) | Delete an index from a collection |
Aggregation Framework
Command | Description |
---|---|
db.<collection>.aggregate([ <stage1>, <stage2>, ... ]) | Perform an aggregation pipeline on a collection |
{ $match: { <filter> } } | Filter documents that match a condition |
{ $group: { _id: <field>, <agg1>: { $<operator>: <field> }, ... } } | Group documents by a field and perform aggregation operations |
{ $sort: { <field>: 1 } } | Sort documents by a field in ascending order |
{ $sort: { <field>: -1 } } | Sort documents by a field in descending order |
{ $limit: <number> } | Limit the number of documents returned |
{ $skip: <number> } | Skip a number of documents |
Miscellaneous Commands
Command | Description |
---|---|
db.<collection>.renameCollection(<newName>) | Rename a collection |
db.<collection>.stats() | Show statistics for a collection |
db.<collection>.explain().find(<filter>) | Show the query plan for a find operation |
db.<collection>.watch() | Watch for changes to a collection |
db.<collection>.aggregate([ { $changeStream: { fullDocument: ""updateLookup"" } } ]) | Watch for changes to a collection using the aggregation framework |
Conclusion
This cheat sheet provides a quick reference guide for MongoDB commands and syntax. Use it as a starting point for your MongoDB development and administration tasks.
Reference: