đź“ť Documentation
đź’ˇ Reference
đź” Glossary
mongosh
# show databases
show dbs
# to switch databases
use <db>
# display the current database
db
# show collections
show collections
CRUD operations create, read, update, and delete documents.
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.
# inserts a new document into a collection
db.collection.insertOne(<document>)
# inserts several new documents into a collection
db.collection.insertMany([ <document 1> , <document 2>, ... ])
Query a collection for documents.
# performs a query on a collection or a view and returns a cursor object
db.collection.find(query, projection)
Parameter | Type | Description |
---|---|---|
query | document | Optional. Specifies selection filter using query operators. |
projection | document | Optional. Specifies the fields to return in the documents that match the query filter. |
Modify existing documents in a collection.
# modifies a single document in a collection
db.collection.updateOne(<filter>, <update>, options)
# modifies multiple documents in a collection
db.collection.updateMany(<filter>, <update>, options)
# replaces a single document in a collection
db.collection.replaceOne(<filter>, <replacement>, options)
Parameter | Type | Description |
---|---|---|
filter | document | The selection criteria for the update. The same query selectors as in the find()  method are available. |
update | document or pipeline | The modifications to apply. |
replacement | document | The replacement document. |
Remove documents from a collection.
# deletes a single document in a collection
db.collection.deleteOne(<filter>)
# deletes multiple documents in a collection
db.collection.deleteMany(<filter>)
Parameter | Type | Description |
---|---|---|
filter | document | Specifies deletion criteria using query operators. |