Connect to a MongoDB instance using the mongo
shell:
mongo
To connect to a specific host and port:
mongo --host <hostname> --port <port>
A quick reference guide to MongoDB commands, syntax, and operations for developers and database administrators.
Connect to a MongoDB instance using the
To connect to a specific host and port:
|
Connect to a MongoDB instance with authentication:
|
|
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. |
|
Lists all collections in the current database. |
|
Creates a new collection in the current database. |
|
Deletes the specified collection. |
Insert a single document:
|
Insert multiple documents:
|
Find all documents in a collection:
|
Find documents with a specific condition:
|
Find a single document that matches the condition:
|
Using operators:
|
Update a single document:
|
Update multiple documents:
|
Replace a single document:
|
Delete a single document:
|
Delete multiple documents:
|
|
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.
|
|
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.
|
|
Matches documents that have the specified field.
|
|
Selects documents where values match a specified BSON type.
|
Create an index on a field:
(1 for ascending, -1 for descending) |
List all indexes for a collection:
|
Drop an index:
|
An example aggregation pipeline:
|
|
|
|
|
|
|