/
query.txt
71 lines (47 loc) · 2 KB
/
query.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
======
$query
======
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. operator:: $query
.. include:: /includes/extracts/mongo-shell-deprecated-meta-operator-query.rst
The :operator:`$query` operator forces MongoDB to interpret an expression
as a query.
The following :binary:`~bin.mongo` operations are equivalent, and
return only those documents in the collection named ``collection`` where the
``age`` field equals ``25``.
.. code-block:: javascript
db.collection.find( { $query: { age : 25 } } )
db.collection.find( { age : 25 } )
:operator:`$query` is necessary to work with documents that contain a field
name ``query`` whose value is an embedded document, such as the following document:
.. code-block:: javascript
{ _id: 1, age: 25, query: { a: 1 } }
The following find operation that does not use the :operator:`$query`
operator will return no results:
.. code-block:: javascript
db.documents.find( { query: { a: 1 } } )
To obtain the document, you will need to use the following query:
.. code-block:: javascript
db.documents.find( { "$query": { query: { a: 1 } } } )
.. seealso:: For more information about queries in MongoDB see
:doc:`/tutorial/query-documents`,
:method:`db.collection.find()`, and
:doc:`Getting Started with MongoDB </tutorial/getting-started>`.
.. note::
Do not mix query forms. If you use the :operator:`$query`
format, do not append :ref:`cursor methods
<js-query-cursor-methods>` to the
:method:`~db.collection.find()`. To modify the query use the
:doc:`meta-query operators </reference/operator/query-modifier>`,
such as :operator:`$explain`.
Therefore, the following two operations are equivalent:
.. code-block:: javascript
db.collection.find( { $query: { age : 25 }, $explain: true } )
db.collection.find( { age : 25 } ).explain()