The Ultimate MongoDB Quick Reference Guide
Essential Commands and Tips for Efficient Database Management
Introduction
MongoDB, a leading NoSQL database, has gained immense popularity among developers and data professionals due to its flexibility and scalability. Whether you're a seasoned MongoDB user or just getting started, having a handy cheat sheet can greatly streamline your development process.
In this article, we present a comprehensive MongoDB cheatsheet that will serve as your go-to resource, providing quick and concise references for the most commonly used commands, query operators, data modeling techniques, and best practices. Whether you need a refresher on basic CRUD operations, want to optimize your queries, or explore advanced features, this cheat sheet will equip you with the knowledge you need to work efficiently with MongoDB and unlock its full potential.
If you want to learn more about MongoDB, including installation instructions and creating and interacting with databases, be sure to check out my comprehensive beginner's guide.
MongoDB commands sheet
Let’s go straight to the point.
Connect to a MongoDB instance using mongosh
Action | Command |
Connect to a MongoDB instance | mongosh "mongodb://USERNAME:PASSWORD@HOST:PORT/DBNAME" |
Here's a breakdown of the command and its components:
mongosh
: This is the command to launch the MongoDB shell. It is a powerful interactive JavaScript shell that allows you to interact with MongoDB databases."mongodb://USERNAME:PASSWORD@HOST:PORT/DBNAME"
: This is the connection string that specifies the details of the MongoDB server you want to connect to. Let's break it down further:USERNAME
andPASSWORD
: These are the credentials (username and password) required to authenticate and gain access to the MongoDB server. You should replace them with the actual username and password for your MongoDB deployment.HOST
: This represents the hostname or IP address of the MongoDB server you want to connect to. It could be a local address (e.g.,localhost
) or a remote address.PORT
: This is the port number on which the MongoDB server is listening for incoming connections. The default MongoDB port is27017
, but it can be different if you've configured your server to use a different port.DBNAME
: This is the name of the MongoDB database you want to connect to. Replace it with the name of the database you wish to access.
To use the command, you would replace the placeholders (USERNAME, PASSWORD, HOST, PORT, and DBNAME) with the appropriate values specific to your MongoDB server configuration.
Working with databases
These commands are used within the MongoDB shell or in scripts interacting with MongoDB. They allow you to manage databases, collections and perform various operations on your data.
Action | Command |
Switch to a database | use DBNAME |
Show current database | db |
List all databases | show dbs |
Create a new collection | db.createCollection("collName") |
Drop current database | db.dropDatabase() |
Working with collections
These commands are commonly used when working with MongoDB to interact with collections, perform CRUD (Create, Read, Update, Delete) operations, and manage the database's structure.
Action | Command |
List all collections | show collections |
Retrieve all documents | db.COLLECTION_NAME.find() |
Insert a single document | db.COLLECTION_NAME.insertOne({...}) |
Insert multiple documents | db.COLLECTION_NAME.insertMany([{...}, {...}, ...]) |
Update a single document | db.COLLECTION_NAME.updateOne({query}, {$set: {update}}) |
Update multiple documents | db.COLLECTION_NAME.updateMany({query}, {$set: {update}}) |
Delete a single document | db.COLLECTION_NAME.deleteOne({query}) |
Delete multiple documents | db.COLLECTION_NAME.deleteMany({query}) |
Drop a collection | db.COLLECTION_NAME.drop() |
Querying and filtering documents
These commands help retrieve specific documents from a collection based on the specified criteria.
Action | Command |
Find exact match | db.COLLECTION_NAME.find({field: value}) |
Find a value greater than | db.COLLECTION_NAME.find({field: {$gt: value}}) |
Find value less than | db.COLLECTION_NAME.find({field: {$lt: value}}) |
Find matching any condition | db.COLLECTION_NAME.find({$or: [{field1: value1}, {field2: value2}]}) |
Find a value in an array | db.COLLECTION_NAME.find({field: {$in: [value1, value2, ...]}}) |
Sorting, limiting, and skipping results
These commands are used to query and manipulate data within a specific collection.
Action | Command |
Sort results ascending | db.COLLECTION_NAME.find().sort({field: 1}) |
Sort results descending | db.COLLECTION_NAME.find().sort({field: -1}) |
Limit results | db.COLLECTION_NAME.find().limit(number) |
Skip results | db.COLLECTION_NAME.find().skip(number) |
Aggregation and grouping
This command is used to perform aggregation operations on a specific collection within a MongoDB database. Aggregation in MongoDB allows you to process and analyze data by performing various operations, such as grouping, filtering, sorting, and transforming documents.
Action | Command |
Aggregate documents | db.COLLECTION_NAME.aggregate([...]) |
Indexing
These commands are part of the MongoDB query language and administration tools, allowing you to manage indexes within your MongoDB database effectively.
Conclusion
Whether you need a quick reference for basic CRUD operations or want to optimize queries and explore advanced features, this cheat sheet equips you with the knowledge to work efficiently with MongoDB. It serves as a go-to resource, helping you unlock the full potential of MongoDB.
Additionally, if you're new to MongoDB, be sure to check out the comprehensive beginner's guide. It covers topics such as installation instructions, creating databases, and interacting with MongoDB. This guide serves as a helpful starting point for beginners looking to dive into MongoDB.