@@ -6,18 +6,118 @@ db.collection.find()
6
6
7
7
.. method:: db.collection.find(query,projection)
8
8
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 .
12
12
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:
16
15
17
- :returns: A cursor whose iteration visits all of the documents that
18
- match the ``query`` document.
16
+ :param document query:
19
17
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