MongoDB CRUD Operations
On this page
CRUD operationscreate,read,update, anddeletedocuments.
Create Operations
Create or insert operations add newdocumentsto acollection. If the collection does not currently exist, insert operations will create the collection.
MongoDB provides the following methods to insert documents into a collection:
db.collection.insertOne()
New in version 3.2db.collection.insertMany()
New in version 3.2
In MongoDB, insert operations target a singlecollection. All write operations in MongoDB areatomicon the level of a singledocument.
For examples, seeInsert Documents.
Read Operations
Read operations retrievesdocumentsfrom acollection; i.e. queries a collection for documents. MongoDB provides the following methods to read documents from a collection:
You can specifyquery filters or criteriathat identify the documents to return.
For examples, see:
- Query Documents
- Query on Embedded/Nested Documents
- Query an Array
- Query an Array of Embedded Documents
Update Operations
Update operations modify existingdocumentsin acollection. MongoDB provides the following methods to update documents of a collection:
db.collection.updateOne()
New in version 3.2db.collection.updateMany()
New in version 3.2db.collection.replaceOne()
New in version 3.2
In MongoDB, update operations target a single collection. All write operations in MongoDB areatomicon the level of a single document.
You can specify criteria, or filters, that identify the documents to update. Thesefiltersuse the same syntax as read operations.
For examples, seeUpdate Documents.
Delete Operations
Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:
db.collection.deleteOne()
New in version 3.2db.collection.deleteMany()
New in version 3.2
In MongoDB, delete operations target a singlecollection. All write operations in MongoDB areatomicon the level of a single document.
You can specify criteria, or filters, that identify the documents to remove. Thesefiltersuse the same syntax as read operations.
For examples, seeDelete Documents.
Bulk Write
MongoDB provides the ability to perform write operations in bulk. For details, seeBulk Write Operations.