The Ultimate MongoDB Quick Reference Guide

The Ultimate MongoDB Quick Reference Guide

Essential Commands and Tips for Efficient Database Management

·

5 min read

Play this article

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

ActionCommand
Connect to a MongoDB instancemongosh "mongodb://USERNAME:PASSWORD@HOST:PORT/DBNAME"

Here's a breakdown of the command and its components:

  1. 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.

  2. "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 and PASSWORD: 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 is 27017, 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.

ActionCommand
Switch to a databaseuse DBNAME
Show current databasedb
List all databasesshow dbs
Create a new collectiondb.createCollection("collName")
Drop current databasedb.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.

ActionCommand
List all collectionsshow collections
Retrieve all documentsdb.COLLECTION_NAME.find()
Insert a single documentdb.COLLECTION_NAME.insertOne({...})
Insert multiple documentsdb.COLLECTION_NAME.insertMany([{...}, {...}, ...])
Update a single documentdb.COLLECTION_NAME.updateOne({query}, {$set: {update}})
Update multiple documentsdb.COLLECTION_NAME.updateMany({query}, {$set: {update}})
Delete a single documentdb.COLLECTION_NAME.deleteOne({query})
Delete multiple documentsdb.COLLECTION_NAME.deleteMany({query})
Drop a collectiondb.COLLECTION_NAME.drop()

Querying and filtering documents

These commands help retrieve specific documents from a collection based on the specified criteria.

ActionCommand
Find exact matchdb.COLLECTION_NAME.find({field: value})
Find a value greater thandb.COLLECTION_NAME.find({field: {$gt: value}})
Find value less thandb.COLLECTION_NAME.find({field: {$lt: value}})
Find matching any conditiondb.COLLECTION_NAME.find({$or: [{field1: value1}, {field2: value2}]})
Find a value in an arraydb.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.

ActionCommand
Sort results ascendingdb.COLLECTION_NAME.find().sort({field: 1})
Sort results descendingdb.COLLECTION_NAME.find().sort({field: -1})
Limit resultsdb.COLLECTION_NAME.find().limit(number)
Skip resultsdb.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.

ActionCommand
Aggregate documentsdb.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.

Did you find this article valuable?

Support Davide by becoming a sponsor. Any amount is appreciated!