@@ -12,210 +12,16 @@ CRUD Operations
1212 /fundamentals/crud/query-document
1313 /fundamentals/crud/compound-operations
1414
15- Overview
16- --------
17-
1815CRUD (Create, Read, Update, Delete) operations allow you to work with
19- the data stored inside of MongoDB. We categorized the CRUD operations into
20- two sections: :ref:`crud-read-operations` and :ref:`crud-write-operations`.
21-
22- .. note::
23-
24- CRUD operations execute **asynchronously** because the driver
25- communicates with MongoDB over the network, which is subject to
26- latency, timeouts, and other complications that can delay execution
27- during synchronous communication. You can specify a callback in the
28- CRUD method call to capture the response. If you do not specify a
29- callback, the operation returns a
30- :mdn:`Promise <Web/JavaScript/Reference/Global_Objects/Promise>` that
31- resolves to the output of the operation. We use the ``async``/``await``
32- syntax for Promise resolution in our examples.
33-
34- .. _crud-read-operations:
35-
36- Read Operations
37- ---------------
38-
39- Read operations find and return documents stored within MongoDB.
40- There are multiple types of read operation that allow you to
41- access data in different ways. While some read operations focus on
42- allowing you to view data that exists in the database, other operations
43- let you monitor the database for new data that matches certain criteria,
44- and still other read operations let you apply a series of
45- transformations and filters to all documents in a collection before
46- returning a result set. The driver offers methods for each variant of
47- read operation:
48-
49- Find
50- ~~~~
51-
52- If you want to view documents already in a collection, you can use
53- :doc:`find() </usage-examples/find>` and
54- :doc:`findOne() </usage-examples/findOne>`. These methods accept a
55- query document that describes the documents you would like to view.
56- While ``findOne()`` returns a single document, ``find()`` returns a
57- :doc:`cursor </fundamentals/crud/read-operations/cursor>` that you can
58- use to navigate matched documents.
59-
60- .. example::
61-
62- A pizza restaurant wants to find all pizzas ordered by Lemony Snicket
63- yesterday. They run the following ``find()`` query on the
64- ``orders`` collection:
65-
66- .. literalinclude:: /code-snippets/crud/pizza.js
67- :language: javascript
68- :start-after: start find crud example
69- :end-before: end find crud example
70- :dedent: 4
71-
72- Aggregate
73- ~~~~~~~~~
74-
75- If you want to create custom processing pipelines for documents in a
76- collection, you can use ``aggregate()``. This method accepts a
77- pipeline of aggregation commands to run in sequence. These commands let
78- you filter, summarize, and augment documents in a collection into a result
79- set for viewing.
80-
81- .. example::
82-
83- A pizza restaurant wants to run a status report on-demand to
84- summarize pizza orders over the past week. They run the following
85- ``aggregate()`` query on the ``orders`` collection:
86-
87- .. literalinclude:: /code-snippets/crud/pizza.js
88- :language: javascript
89- :start-after: start aggregate crud example
90- :end-before: end aggregate crud example
91- :dedent: 4
92-
93- .. _watch-subscribe:
94-
95- Watch / Subscribe
96- ~~~~~~~~~~~~~~~~~
97-
98- You can use the ``watch()`` method if you want to monitor a collection for
99- new, update, replace, and deleted documents that match a certain criteria.
100- You can pass this method a pipeline of aggregation commands that
101- sequentially run on new data whenever write operations run on the collection.
102-
103- .. example::
104-
105- A pizza restaurant wants to get a notification whenever a new pizza
106- order comes in. They run the following ``watch()`` query on the
107- ``orders`` collection:
108-
109- .. literalinclude:: /code-snippets/crud/pizza.js
110- :language: javascript
111- :start-after: start watch crud example
112- :end-before: end watch crud example
113- :dedent: 4
114-
115- .. note::
116-
117- You can customize read operations to
118- :doc:`skip </fundamentals/crud/read-operations/skip>` a certain
119- number of documents at the beginning of the collection of matched
120- documents, :doc:`limit </fundamentals/crud/read-operations/limit>`
121- the number of returned documents, and :doc:`sort
122- </fundamentals/crud/read-operations/sort>` returned documents in a
123- particular order.
124-
125- .. _crud-write-operations:
126-
127- Write Operations
128- ----------------
129-
130- CRUD write operations add, modify, and remove data in MongoDB. Sometimes
131- this means putting entirely new data into a collection, but other times
132- it involves changing data that already exists, or even removing it
133- entirely. There are three distinct write operations: create, update, and
134- delete. The driver offers methods for each variant of write operation:
135-
136- Insert
137- ~~~~~~
138-
139- If you want to add new documents to a collection, you can use
140- :doc:`insertOne() </usage-examples/insertOne>` or
141- :doc:`insertMany() </usage-examples/insertMany>`. These methods accept a
142- single document or a collection of documents respectively. Note that the
143- driver will generate a unique ``_id`` field automatically for any
144- inserted documents that lack such a field.
145-
146- .. example::
147-
148- A pizza restaurant creates an order every time a customer requests
149- a pizza. They run the following ``insertOne()`` query on the
150- ``orders`` collection:
151-
152- .. literalinclude:: /code-snippets/crud/pizza.js
153- :language: javascript
154- :start-after: start insert crud example
155- :end-before: end insert crud example
156- :dedent: 4
157-
158- Update / Replace
159- ~~~~~~~~~~~~~~~~
160-
161- If you want to alter existing documents in a collection, you can use
162- :doc:`updateOne() </usage-examples/updateOne>`,
163- :doc:`replaceOne() </usage-examples/replaceOne>`, or
164- :doc:`updateMany() </usage-examples/updateMany>`. These methods
165- accept a query document that describes the documents you would like to
166- change and an `update document <updateDocument>`_ that describes
167- the changes you would like to apply to matched documents.
168- ``replaceOne()`` uses a `replacement document
169- <replacementDocument>`_ that describes an entirely new document instead
170- of an update document. You can configure any update operation to behave
171- as an :doc:`upsert </fundamentals/crud/write-operations/upsert>` instead
172- of an update.
173-
174- .. example::
175-
176- A pizza restaurant wants to modify the delivery address for an order
177- that already exists. They run the following ``updateOne()`` query on
178- the ``orders`` collection:
179-
180- .. literalinclude:: /code-snippets/crud/pizza.js
181- :language: javascript
182- :start-after: start update crud example
183- :end-before: end update crud example
184- :dedent: 4
185-
186- Delete
187- ~~~~~~
188-
189- If you want to remove existing documents from a collection, you can
190- use :doc:`deleteOne() </usage-examples/deleteOne>` or
191- :doc:`deleteMany() </usage-examples/deleteMany>`. These methods accept a
192- query document that describes the documents you would like to delete.
193- Delete operations will delete either one (in the case of
194- ``deleteOne()``) or all (in the case of ``deleteMany()``) documents that
195- match the query.
196-
197- .. example::
198-
199- A pizza restaurant wants to delete a cancelled order. They run the
200- following ``deleteOne()`` query on the ``orders`` collection:
201-
202- .. literalinclude:: /code-snippets/crud/pizza.js
203- :language: javascript
204- :start-after: start delete crud example
205- :end-before: end delete crud example
206- :dedent: 4
207-
208- .. note::
16+ the data stored in MongoDB.
20917
210- You can customize write operations to
211- :doc:`upsert </fundamentals/crud/write-operations/upsert>` instead of
212- update, time out if the method fails to complete within a certain
213- period of time, and write with varying levels of resilience in a
214- replica set.
18+ The CRUD operation documentation is categorized in two sections:
21519
216- Compound operations
217- -------------------
20+ - :doc:`Read Operations </fundamentals/crud/read-operations>` find and return
21+ documents stored within your MongoDB database.
22+ - :doc:`Write Operations </fundamentals/crud/write-operations>` insert, modify,
23+ or delete documents in your MongoDB database.
21824
219- Some operations combine aspects of read and write operations. Consult
220- :doc:`compound operations </fundamentals/crud/compound-operations>` to
221- learn more about these hybrid methods.
25+ Some operations combine aspects of read and write operations. See our
26+ guide on :doc:`compound operations </fundamentals/crud/compound-operations>`
27+ to learn more about these hybrid methods.
0 commit comments