Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
7 contributors

Users who have contributed to this file

@tychoish @kay-kim @jdestefano-mongo @i80and @spf13 @runME @schmalliso
140 lines (94 sloc) 3.85 KB
.. _crud:
=======================
MongoDB CRUD Operations
=======================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
CRUD operations *create*, *read*, *update*, and *delete*
:ref:`documents <bson-document-format>`.
Create Operations
-----------------
Create or insert operations add new :ref:`documents
<bson-document-format>` to a :ref:`collection <collections>`. If the
collection does not currently exist, insert operations will create the
collection.
MongoDB provides the following methods to insert documents into a
collection:
- :method:`db.collection.insertOne()` |versionadded|
- :method:`db.collection.insertMany()` |versionadded|
In MongoDB, insert operations target a single :term:`collection`. All
write operations in MongoDB are :doc:`atomic
</core/write-operations-atomicity>` on the level of a single
:doc:`document </core/document>`.
.. include:: /images/crud-annotated-mongodb-insertOne.rst
For examples, see :doc:`/tutorial/insert-documents`.
.. _crud-read-operations:
Read Operations
---------------
Read operations retrieves :ref:`documents <bson-document-format>` from a
:ref:`collection <collections>`; i.e. queries a collection for
documents. MongoDB provides the following methods to read documents from
a collection:
- :method:`db.collection.find()`
You can specify :ref:`query filters or criteria
<read-operations-query-argument>` that identify the documents to return.
.. include:: /images/crud-annotated-mongodb-find.rst
For examples, see:
- :doc:`/tutorial/query-documents`
- :doc:`/tutorial/query-embedded-documents`
- :doc:`/tutorial/query-arrays`
- :doc:`/tutorial/query-array-of-documents`
Update Operations
-----------------
Update operations modify existing :ref:`documents
<bson-document-format>` in a :ref:`collection <collections>`. MongoDB
provides the following methods to update documents of a collection:
- :method:`db.collection.updateOne()` |versionadded|
- :method:`db.collection.updateMany()` |versionadded|
- :method:`db.collection.replaceOne()` |versionadded|
In MongoDB, update operations target a single collection. All write
operations in MongoDB are :doc:`atomic
</core/write-operations-atomicity>` on the level of a single document.
You can specify criteria, or filters, that identify the documents to
update. These :ref:`filters <document-query-filter>` use the same
syntax as read operations.
.. include:: /images/crud-annotated-mongodb-updateMany.rst
For examples, see :doc:`/tutorial/update-documents`.
Delete Operations
-----------------
Delete operations remove documents from a collection. MongoDB provides
the following methods to delete documents of a collection:
- :method:`db.collection.deleteOne()` |versionadded|
- :method:`db.collection.deleteMany()` |versionadded|
In MongoDB, delete operations target a single :term:`collection`. All
write operations in MongoDB are :doc:`atomic
</core/write-operations-atomicity>` on the level of a single document.
You can specify criteria, or filters, that identify the documents to
remove. These :ref:`filters <document-query-filter>` use the same
syntax as read operations.
.. include:: /images/crud-annotated-mongodb-deleteMany.rst
For examples, see :doc:`/tutorial/remove-documents`.
Bulk Write
----------
MongoDB provides the ability to perform write operations in bulk. For
details, see :doc:`/core/bulk-write-operations`.
.. toctree::
:titlesonly:
:hidden:
/tutorial/insert-documents
/tutorial/query-documents
/tutorial/update-documents
/tutorial/remove-documents
/core/bulk-write-operations
/core/retryable-writes
/reference/sql-comparison
/text-search
/geospatial-queries
Read Isolation (Read Concern) </reference/read-concern>
Write Acknowledgement (Write Concern)</reference/write-concern>
/core/crud
.. |versionadded| replace:: *New in version 3.2*
You can’t perform that action at this time.