Catalog / MongoDB Cheat Sheet
MongoDB Cheat Sheet
A quick reference guide to MongoDB commands, syntax, and operations for developers and database administrators.
Basic Operations
Connecting to MongoDB
Connect to a MongoDB instance using the
To connect to a specific host and port:
|
Connect to a MongoDB instance with authentication:
|
Database Operations
|
Lists all the databases available on the server. |
|
Switches to the specified database. Creates the database if it doesn’t exist. |
|
Displays the current database you are using. |
|
Deletes the current database. |
Collection Operations
|
Lists all collections in the current database. |
|
Creates a new collection in the current database. |
|
Deletes the specified collection. |
CRUD Operations
Insert Operations
Insert a single document:
|
Insert multiple documents:
|
Query Operations
Find all documents in a collection:
|
Find documents with a specific condition:
|
Find a single document that matches the condition:
|
Using operators:
|
Update Operations
Update a single document:
|
Update multiple documents:
|
Replace a single document:
|
Delete Operations
Delete a single document:
|
Delete multiple documents:
|
Querying with Operators
Comparison Operators
|
Matches values that are equal to a specified value.
|
|
Matches values that are greater than a specified value.
|
|
Matches values that are greater than or equal to a specified value.
|
|
Matches values that are less than a specified value.
|
|
Matches values that are less than or equal to a specified value.
|
|
Matches all values that are not equal to a specified value.
|
|
Matches any of the values specified in an array.
|
|
Matches none of the values specified in an array.
|
Logical Operators
|
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
|
|
Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
|
|
Inverts the effect of a query expression and returns documents that do not match the query expression.
|
|
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
|
Element Operators
|
Matches documents that have the specified field.
|
|
Selects documents where values match a specified BSON type.
|
Indexes and Aggregation
Index Operations
Create an index on a field:
(1 for ascending, -1 for descending) |
List all indexes for a collection:
|
Drop an index:
|
Aggregation Pipeline
An example aggregation pipeline:
|
|
|
|
|
|
|