MongoDB, a popular NoSQL database, offers high performance, high availability, and easy scalability. This cheat sheet provides a comprehensive guide to MongoDB commands, queries, and operators, making it a valuable resource for both beginners and advanced users.
Introduction to MongoDB
MongoDB Commands Cheat Sheet
Basic Commands
- Start MongoDB Shell
- Command: mongo
- Description: Launch the MongoDB shell to interact with the database.
- Connecting to MongoDB
- Command: mongo --host --port
- Description: Connect to a MongoDB instance
- Show Databases
- Command: show dbs
- Description: List all databases on the server.
- Use a Database
- Command: use [database_name]
- Description: Switch to a specific database.
- Example: use myDatabase
- Show Collections
- Command: show collections
- Description: List all collections in the current database.
- Create a Collection
- Command: db.createCollection("[collection_name]")
- Description: Create a new collection.
- Example: db.createCollection("myCollection")
- Drop a Collection
- Command: db.[collection_name].drop()
- Description: Remove a collection from the database.
- Example: db.myCollection.drop()
- Drop a Database
- Command: db.dropDatabase()
- Description: Drop the current database.
Data Insertion Commands
- Insert One Document
- Command: db.[collection_name].insertOne()
- Description: Insert a single document into a collection.
- Example: db.users.insertOne()
- Insert Multiple Documents
- Command: db.[collection_name].insertMany([, ])
- Description: Insert multiple documents into a collection.
- Example: db.users.insertMany([, ])
MongoDB Query Cheat Sheet
Basic Queries
- Find One Document
- Command: db.[collection_name].findOne()
- Description: Retrieve a single document that matches the query.
- Example: db.users.findOne()
- Find Multiple Documents
- Command: db.[collection_name].find()
- Description: Retrieve all documents that match the query.
- Example: db.users.find(>)
Query Operators
MongoDB Aggregation Cheat Sheet
Aggregation operations process data records and return computed results. These operations group values from multiple documents together and can perform various operations on the grouped data to return a single result.
Basic Aggregation Commands
MongoDB Shell Commands Cheat Sheet
- List Databases
- Command: show dbs
- Example: show dbs
- List Collections
- Command: show collections
- Example: show collections
- Switch Database
- Command: use [database_name]
- Example: use myDatabase
- Create Collection
- Command: db.createCollection("[collection_name]")
- Example: db.createCollection("newCollection")
MongoDB Compass Query Cheat Sheet
MongoDB Compass provides a graphical interface to manage MongoDB databases. Here's how to perform some basic queries:
- Find All Documents
- Use the "Find" tab to enter an empty query <>.
- Filter Documents
- Use the query bar to enter queries such as >.
- Aggregation Pipeline
- Use the "Aggregation" tab to build aggregation pipelines visually.
MongoDB Syntax Cheat Sheet
CRUD Operations
Create
- db.[collection].insertOne(): Inserts a single document into a collection.
- db.[collection].insertMany([, , . ]): Inserts multiple documents into a collection.
Read
- db.[collection].find(): Finds all documents that match the query. Returns all documents if the query is empty.
- db.[collection].findOne(): Finds a single document that matches the query.
- db.[collection].find().limit(number): Limits the number of documents returned.
Update
- db.[collection].updateOne(, , ): Updates a single document that matches the filter.
- db.[collection].updateMany(, , ): Updates multiple documents that match the filter.
- db.[collection].replaceOne(, , ): Replaces a single document that matches the filter with a new document.
Delete
- db.[collection].deleteOne(): Deletes a single document that matches the filter.
- db.[collection].deleteMany(): Deletes multiple documents that match the filter.
MongoDB Operators Cheat Sheet
Comparison Operators
- $eq: Matches values that are equal to a specified value.
- $gt: Matches values that are greater than a specified value.
- $lt: Matches values that are less than a specified value.
Logical Operators
Advanced MongoDB Queries Cheat Sheet
Indexing
- db.[collection].createIndex(): Creates an ascending index on the specified field.
- db.[collection].createIndex(): Creates a descending index on the specified field.
- db.[collection].getIndexes(): Lists all indexes on the collection.
- db.[collection].dropIndex("indexName"): Drops the specified index.
User Management
- db.createUser(): Creates a new user with specified roles.
- db.updateUser("username", ): Updates the password for an existing user.
- db.dropUser("username"): Deletes a user.
Backup and Restore
- mongodump --db [database] --out [directory]: Creates a backup of the specified database.
- mongorestore --db [database] [directory]: Restores the specified database from a backup.
Conclusion
This MongoDB cheat sheet serves as a quick reference guide for developers and database administrators. It covers essential commands, query syntax, aggregation operations, and more to help you manage and interact with your MongoDB databases efficiently. Keep this cheat sheet handy to streamline your workflow and enhance your productivity with MongoDB.
Made with in Bengaluru, India
- Contact Us
- admissions@almabetter.com
- 08046008400
- Official Address
- 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
- Communication Address
- 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034