Skip to content

Commit 74cd31f

Browse files
committed
DOCS-555 update the main crud reference docs
1 parent 06408ea commit 74cd31f

File tree

9 files changed

+616
-88
lines changed

9 files changed

+616
-88
lines changed

draft/applications/create.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
======
2+
Create
3+
======
4+
5+
.. default-domain:: mongodb
6+
7+
Create operation adds new :term:`documents <document>` to a
8+
:term:`collection`. MongoDB provides the :ref:`insert() <insert>`
9+
method to perform create operations. Additionally, the :ref:`update()
10+
with upsert option <update_and_save>` method and the :ref:`save()
11+
<update_and_save>` method also provide functionality to create
12+
documents.
13+
14+
All three methods exhibit the following behavior during the create
15+
operation:
16+
17+
- If the new document does not specify an :term:`_id` field, these
18+
methods will add the ``_id`` field to the document and assign as
19+
its value a unique :term:`objectid`.
20+
21+
- If the document specifies a new field, these methods will add
22+
the document with the new field **without** requiring a schema change
23+
to the collection or any change to the existing documents.
24+
25+
Keep in mind that MongoDB v2.2 has a limit on document size of 16
26+
megabytes (MB).
27+
28+
29+
.. _insert:
30+
31+
Insert()
32+
--------
33+
34+
The :method:`insert() <db.collection.insert()>` method is the primary
35+
method to insert a document or documents into a collection. The
36+
:method:`insert() <db.collection.insert()>` is analogous to the ``SQL
37+
INSERT`` except the :method:`insert() <db.collection.insert()>` method
38+
provides these additional functionalities:
39+
40+
- If the collection does not exist, then the :method:`insert()
41+
<db.collection.insert()>` method will create the collection.
42+
43+
- If the new document does not specify an ``_id`` field, then
44+
:method:`insert() <db.collection.insert()>` will add the ``_id``
45+
field to the document and assign a unique ``objectid`` as its
46+
value.
47+
48+
- If passed an array of documents, the :method:`insert()
49+
<db.collection.insert()>` method can perform bulk inserts into a
50+
collection.
51+
52+
.. _update_and_save:
53+
54+
Update() with Upsert Option and Save()
55+
--------------------------------------
56+
57+
Both the :method:`update() <db.collection.update()>` method and the
58+
:method:`save() <db.collection.save()>` method will be discussed in
59+
fuller detail in the Update section. But a quick introduction to their
60+
insert capabilities is presented here for completeness.
61+
62+
Update() with Upsert Option
63+
````````````````````````````
64+
65+
The :method:`update() with upsert option <db.collection.update()>` or
66+
an ``upsert`` will insert a single document into a collection if no
67+
document exists that matches the update query criteria. The inserted
68+
document will contain the fields and values of both the update query
69+
criteria and the update operation. If the ``_id`` is not specified in
70+
either the query criteria and the update operation documents, the
71+
``upsert`` will add the ``_id`` field to the document and assign a
72+
unique ``objectid`` as its value.
73+
74+
The ``upsert`` removes the need to perform a separate query to
75+
check for the existence of a record before performing either an insert
76+
or an update.
77+
78+
Save()
79+
``````
80+
81+
The :method:`save() <db.collection.save()>` will insert a document into
82+
a :term:`collection` if the document does not contain the ``_id`` field
83+
or contains an ``_id`` field with a value not in the collection. If the
84+
``_id`` is not specified in the document parameter, :method:`save()
85+
<db.collection.save()>` will add the ``_id`` field to the document
86+
and assign a unique ``objectid`` as its value.

draft/applications/delete.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
======
2+
Delete
3+
======
4+
5+
.. default-domain:: mongodb
6+
7+
Delete operation removes documents from a :term:`collection`. MongoDB
8+
provides the :ref:`remove() <remove>` method to perform delete operations.
9+
10+
.. _remove:
11+
12+
Remove()
13+
--------
14+
15+
The :method:`remove() <db.collection.remove()>` method is the method to
16+
delete documents from a collection. By default, the :method:`remove()
17+
<db.collection.remove()>` deletes all documents that meet the delete
18+
selection criteria. However, using the ``justOne`` option limits the
19+
deletion to just one document.
20+
21+
Note that calling :method:`remove() <db.collection.remove()>` without
22+
specifying the delete selection criteria removes all documents in the
23+
collection.
24+

draft/applications/read.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
====
2+
Read
3+
====
4+
5+
.. default-domain:: mongodb
6+
7+
Read operation selects documents from a collection. MongoDB
8+
provides the :ref:`find() <find>` method to perform read operations.
9+
Additionally, the :ref:`Aggregation Framework <aggregation>` also
10+
provides read operations that involve various aggregation functions,
11+
such as ``SUM``.
12+
13+
.. _find:
14+
15+
Find()
16+
------
17+
18+
The :method:`find() <db.collection.find()>` method is the primary
19+
method to select documents from a collection. The find() method returns
20+
a cursor to the selected documents. The :method:`find()
21+
<db.collection.find()>` method can accept a selection criteria on
22+
document fields, including array fields and subdocuments, to limit the
23+
selection.
24+
25+
The :method:`find() <db.collection.find()>` method offers the
26+
flexibility to specify the fields to return through the include/exclude
27+
flags of the ``projection`` document. For instance, if selecting 11 out
28+
of 15 fields, with the :method:`find() <db.collection.find()>` method, you
29+
can choose to specify the 4 to exclude rather than the 11 to include.
30+
31+
.. TODO -- sort/limit/other options...
32+
33+
.. _aggregation:
34+
35+
Aggregation Framework
36+
---------------------
37+
38+
The Aggregation Framework provides a rich set of tools for performing
39+
various sequence of operations, not the least of which is the read operation.
40+
41+
Although the Aggregation Framework is discussed in detail in its own
42+
section, a brief introduction to the aggregation read operation is
43+
provided here for completeness.
44+
45+
.. seealso:: :doc:`/applications/aggregation`

draft/applications/update.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
======
2+
Update
3+
======
4+
5+
.. default-domain:: mongodb
6+
7+
Update operation modifies an existing :term:`document <document>` or
8+
documents in a :term:`collection`. MongoDB provides the :ref:`update()
9+
<update>` method to perform update operations.
10+
Additionally, the :ref:`save() <save>` method also provides the
11+
functionality to update documents.
12+
13+
Note that the two methods also provide the option to insert a new
14+
document into a collection.
15+
16+
.. TODO ? findAndModify?
17+
18+
.. _update:
19+
20+
Update
21+
------
22+
23+
The :method:`update() <db.collection.update()>` method is the primary
24+
method to update an existing document or documents in a collection. The
25+
:method:`update() <db.collection.update()>` method can either replace
26+
the existing document with the new document or update specific fields
27+
in the existing document.
28+
29+
By default the :method:`update() <db.collection.update()>` method
30+
updates a single document, but by using the ``multi`` option, the
31+
method can update all documents that match the update selection
32+
criteria.
33+
34+
Additionally, if no existing document match the update selection
35+
criteria, by using the ``upsert`` option, the method can insert
36+
a new document into the collection.
37+
38+
Note also that the :method:`update() <db.collection.update()>` method
39+
can also modify the name of the ``field`` in a document using the
40+
:operator:`$rename` operator.
41+
42+
.. _save:
43+
44+
Save
45+
----
46+
47+
The :method:`save() <db.collection.save()>` method updates an existing
48+
document or inserts a document depending on the parameter. The
49+
:method:`save() <db.collection.save()>` method is like an ``upsert``
50+
operation that has the update selection criteria on the :term:`_id`
51+
field:
52+
53+
- If no ``_id`` field exists, the :method:`save()
54+
<db.collection.save()>` method performs an insert. The insert will
55+
add the ``_id`` field and assign a unique :term:`objectid` as its
56+
value.
57+
58+
- If ``_id`` field exists but does not match any document in the
59+
collection, the :method:`save() <db.collection.save()>` method
60+
performs an insert. The insert will add the ``_id`` field and assign
61+
a unique :term:`objectid` as its value.
62+
63+
- If ``_id`` field exists and matches an existing document in the
64+
collection, the :method:`save() <db.collection.save()>` method
65+
performs an update that replaces the existing document with the new
66+
document.
67+

source/reference/method/db.collection.find.txt

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,118 @@ db.collection.find()
66

77
.. method:: db.collection.find(query,projection)
88

9-
:param document query: A :term:`document` that specifies the
10-
:term:`query` using the JSON-like syntax and
11-
:doc:`query operators </reference/operators>`.
9+
The :method:`find() <db.collection.find()>` method selects
10+
documents in a collection and returns a
11+
:term:`cursor` to the selected documents.
1212

13-
:param document projection: Optional. A :term:`document` that
14-
controls the :term:`projection`, or the
15-
contents of the data returned.
13+
The :method:`find() <db.collection.find()>` method can take the
14+
following parameters:
1615

17-
:returns: A cursor whose iteration visits all of the documents that
18-
match the ``query`` document.
16+
:param document query:
1917

20-
Queries for documents matching ``query``. The argument to
21-
:method:`find() <db.collection.find>` takes the form of a :term:`document`. See
22-
the ":doc:`/reference/operators`" for an overview of the available
23-
operators for specifying and narrowing the query.
18+
Optional. A document that specifies
19+
the selection criteria using :doc:`query operators
20+
</reference/operators>`. Omit the ``query`` parameter or pass
21+
an empty ``{}`` document to return all documents in the
22+
collection.
23+
24+
:param document projection:
25+
26+
Optional. A document that controls the fields to return, or
27+
the :term:`projection`. The ``projection`` document has
28+
the following syntax:
29+
30+
.. code-block:: javascript
31+
32+
{ field1: boolean, field2: boolean ... }
33+
34+
The ``boolean`` can take the following include/exclude values:
35+
36+
- ``1`` or ``true`` to include. The :method:`find()
37+
<db.collection.find()>` method always includes the
38+
:term:`_id` field even if the field is not explicitly
39+
stated to return in the :term:`projection` parameter.
40+
41+
- ``0`` or ``false`` to exclude.
42+
43+
Currently, you cannot mix include and exclude fields in
44+
the projection document.
45+
46+
:returns:
47+
48+
A :term:`cursor` to the documents that match
49+
the ``query`` criteria and contain the ``projection`` fields.
50+
51+
.. examples-begin
52+
53+
Consider the following examples of the :method:`find()
54+
<db.collection.find()>` method.
55+
56+
- To select all documents in a collection, call the
57+
:method:`find() <db.collection.find()>` method with no parameters:
58+
59+
.. code-block:: javascript
60+
61+
db.products.find()
62+
63+
The query will return all the documents with all the fields from
64+
the collection ``products``. By default, in the :program:`mongo`
65+
shell, the cursor returns the first batch of 20 matching
66+
documents. In the :program:`mongo` shell, iterate through the next
67+
batch by typing ``it``. Use the appropriate cursor
68+
handling mechanism for your specific language driver.
69+
70+
- To select the documents that match a selection criteria, call the
71+
:method:`find() <db.collection.find()>` method with the ``query``
72+
criteria:
73+
74+
.. code-block:: javascript
75+
76+
db.products.find( { qty: { $gt: 25 } } )
77+
78+
The query will return all the documents from the collection
79+
``products`` where ``qty`` is greater than ``25``. The resulting
80+
documents will contain all their respective fields.
81+
82+
- To select the documents that match a selection criteria and return
83+
only certain fields, call the :method:`find()
84+
<db.collection.find()>` method with the ``query`` criteria and the
85+
``projection`` parameter using the ``include`` syntax:
86+
87+
.. code-block:: javascript
88+
89+
db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )
90+
91+
The query will return all the documents from the collection
92+
``products`` where ``qty`` is greater than ``25``. The resulting
93+
documents will contain only the ``_id``, ``item``, and ``qty``
94+
fields. Note that the ``_id`` field is returned even without
95+
explicitly including it.
96+
97+
.. code-block:: javascript
98+
99+
{ "_id" : 11, "item" : "pencil", "qty" : 50 }
100+
{ "_id" : ObjectId("50634d86be4617f17bb159cd"), "item" : "bottle", "qty" : 30 }
101+
{ "_id" : ObjectId("50634dbcbe4617f17bb159d0"), "item" : "paper", "qty" : 100 }
102+
103+
- To select the documents that match a selection criteria and
104+
exclude certain fields from the resulting documents, call the
105+
:method:`find() <db.collection.find()>` method with the ``query``
106+
criteria and the ``projection`` parameter using the ``exclude`` syntax:
107+
108+
.. code-block:: javascript
109+
110+
db.products.find( { qty: { $gt: 25 } }, { _id: 0, qty: 0 } )
111+
112+
The query will return all the documents from the collection
113+
``products`` where ``qty`` is greater than ``25``. The resulting
114+
documents will contain all fields **except** the ``_id`` and
115+
``qty`` fields.
116+
117+
.. code-block:: javascript
118+
119+
{ "item" : "pencil", "type" : "no.2" }
120+
{ "item" : "bottle", "type" : "blue" }
121+
{ "item" : "paper" }
122+
123+
.. examples-end

0 commit comments

Comments
 (0)