Skip to content

Commit c59d23f

Browse files
author
Chris Cho
authored
DOCSP-10482: rearrange CRUD landing page content (#111)
* DOCSP-10482: rearrange CRUD landing page content
1 parent a1e7445 commit c59d23f

File tree

6 files changed

+309
-204
lines changed

6 files changed

+309
-204
lines changed

source/fundamentals/crud.txt

Lines changed: 9 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -12,210 +12,16 @@ CRUD Operations
1212
/fundamentals/crud/query-document
1313
/fundamentals/crud/compound-operations
1414

15-
Overview
16-
--------
17-
1815
CRUD (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.

source/fundamentals/crud/read-operations.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Read Operations
77
.. toctree::
88
:caption: Read Operations
99

10+
/fundamentals/crud/read-operations/retrieve
11+
/fundamentals/crud/read-operations/cursor
1012
/fundamentals/crud/read-operations/sort
1113
/fundamentals/crud/read-operations/skip
1214
/fundamentals/crud/read-operations/limit
1315
/fundamentals/crud/read-operations/project
14-
/fundamentals/crud/read-operations/cursor
1516
/fundamentals/crud/read-operations/geo
1617
/fundamentals/crud/read-operations/text
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
=============
2+
Retrieve Data
3+
=============
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
You can use read operations to retrieve data from your MongoDB database.
17+
There are multiple types of read operations that access the data in
18+
different ways. If you want to request results based on a set of criteria
19+
from the existing set of data, you can use a find operation such as the
20+
``find()`` or ``findOne()`` methods.
21+
22+
You can also further specify the information you are requesting by
23+
including additional parameters or by chaining other methods such as:
24+
25+
- :doc:`Sort Results </fundamentals/crud/read-operations/sort>`
26+
- :doc:`Skip Returned Results </fundamentals/crud/read-operations/skip>`
27+
- :doc:`Limit the Number of Returned Results </fundamentals/crud/read-operations/limit>`
28+
- :doc:`Specify Which Fields to Return </fundamentals/crud/read-operations/project>`
29+
30+
You can also use an aggregation operation to retrieve data. This type of
31+
operation allows you to apply an ordered pipeline of transformations to the
32+
matched data.
33+
34+
If you want to monitor the database for incoming data that matches a set of
35+
criteria, you can use the watch operation to be notified in real-time when
36+
matching data is inserted.
37+
38+
Find
39+
----
40+
41+
The ``find()`` method is called on the ``Collection`` object that
42+
references the collection you want to query. The method accepts a query
43+
document that describes the documents you want to retrieve. For more
44+
information on how to specify your query document, see our guide on
45+
how to :doc:`Specify a Query </fundamentals/crud/query-document>`.
46+
47+
To access the results, you can optionally pass a callback in the method
48+
call or resolve the returned ``Promise`` object. See our guide on
49+
:doc:`Promises and Callbacks </fundamentals/promises>` for more
50+
information.
51+
52+
If you resolve the ``Promise`` returned by ``find()``, you receive
53+
a reference to a ``Cursor`` with which you can navigate matched documents.
54+
If you resolve the ``Promise`` returned by ``findOne()``, you receive the
55+
matching document or ``null`` if there are no matches.
56+
57+
.. example::
58+
59+
A pizza restaurant wants to find all pizzas ordered by Lemony Snicket
60+
yesterday. They run the following ``find()`` query on the
61+
``orders`` collection:
62+
63+
.. literalinclude:: /code-snippets/crud/pizza.js
64+
:language: javascript
65+
:start-after: start find crud example
66+
:end-before: end find crud example
67+
:dedent: 4
68+
69+
70+
Once the operation returns, the ``findResult`` variable references a
71+
``Cursor``. You can print the documents retrieved using the ``forEach()``
72+
method as shown below:
73+
74+
.. code-block:: javascript
75+
76+
await cursor.forEach(console.dir);
77+
78+
79+
The output might resemble the following:
80+
81+
.. code-block:: none
82+
83+
[
84+
{ name: "Lemony Snicket", type: "horseradish pizza", qty: 1, status: "delivered", date: ... },
85+
{ name: "Lemony Snicket", type: "coal-fired oven pizza", qty: 3, status: "canceled", date: ...},
86+
...
87+
]
88+
89+
See the :doc:`find() </usage-examples/find>` and :doc:`findOne()
90+
</usage-examples/findOne>` for fully-runnable examples.
91+
92+
Aggregate
93+
---------
94+
95+
If you want to run a custom processing pipeline to retrieve data from your
96+
database, you can use the ``aggregate()`` method. This method accepts
97+
aggregation expressions to run in sequence. These expressions let you filter,
98+
group, and arrange the result data from a collection.
99+
100+
.. example::
101+
102+
A pizza restaurant wants to run a status report on-demand to
103+
summarize pizza orders over the past week. They run the following
104+
``aggregate()`` query on the ``orders`` collection to fetch the
105+
totals for each distinct "status" field:
106+
107+
.. literalinclude:: /code-snippets/crud/pizza.js
108+
:language: javascript
109+
:start-after: start aggregate crud example
110+
:end-before: end aggregate crud example
111+
:dedent: 4
112+
113+
114+
Once the operation returns, the ``aggregateResult`` variable references a
115+
``Cursor``. You can print the documents retrieved using the ``forEach()``
116+
method as shown below:
117+
118+
.. code-block:: javascript
119+
120+
await cursor.forEach(console.dir);
121+
122+
123+
The output might resemble the following:
124+
125+
.. code-block:: none
126+
127+
[
128+
{ _id: 'delivering', count: 5 },
129+
{ _id: 'delivered', count: 37 },
130+
{ _id: 'created', count: 9 },
131+
]
132+
133+
See the MongoDB server manual pages on :manual:`aggregation </aggregation>`
134+
for more information on how to construct an aggregation pipeline.
135+
136+
Watch / Subscribe
137+
-----------------
138+
139+
You can use the ``watch()`` method to monitor a collection for changes to
140+
a collection that match certain criteria. These changes include inserted,
141+
updated, replaced, and deleted documents. You can pass this method
142+
a pipeline of aggregation comands that sequentially runs on the changed
143+
data whenever write operations are executed on the collection.
144+
145+
.. example::
146+
147+
A pizza restaurant wants to receive a notification whenever a new pizza
148+
order comes in. To accomplish this, they create an aggregation pipeline
149+
to filter on insert operations and return specific fields. They pass
150+
this pipeline to the ``watch()`` method called on the ``orders``
151+
collection as shown below:
152+
153+
.. literalinclude:: /code-snippets/crud/pizza.js
154+
:language: javascript
155+
:start-after: start watch crud example
156+
:end-before: end watch crud example
157+
:dedent: 4
158+
159+
160+
For a runnable example of the ``watch()`` method using the NodeJS driver, see
161+
the :doc:`change streams </usage-examples/changeStream>` usage example.

0 commit comments

Comments
 (0)