-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
elemMatch.txt
140 lines (106 loc) · 4.02 KB
/
elemMatch.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
=======================
$elemMatch (projection)
=======================
.. seealso:: :doc:`/reference/operator/query/elemMatch`
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. projection:: $elemMatch
The :projection:`$elemMatch` operator limits the contents of an
``<array>`` field from the query results to contain only the **first**
element matching the :projection:`$elemMatch` condition.
Usage Considerations
--------------------
.. include:: /includes/fact-positional-projection-vs-elemmatch.rst
.. include:: /includes/extracts/views-unsupported-projection-$elemMatch.rst
- You cannot specify a :query:`$text` query expression in an
:projection:`$elemMatch`.
Examples
--------
The examples on the :projection:`$elemMatch` projection operator
assumes a collection ``school`` with the following documents:
.. code-block:: javascript
{
_id: 1,
zipcode: "63109",
students: [
{ name: "john", school: 102, age: 10 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 15 }
]
}
{
_id: 2,
zipcode: "63110",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 3,
zipcode: "63109",
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 4,
zipcode: "63109",
students: [
{ name: "barney", school: 102, age: 7 },
{ name: "ruth", school: 102, age: 16 },
]
}
Zipcode Search
~~~~~~~~~~~~~~
The following :method:`~db.collection.find()` operation
queries for all documents where the value of the ``zipcode``
field is ``63109``. The :projection:`$elemMatch` projection
returns only the **first** matching element of the ``students``
array where the ``school`` field has a value of ``102``:
.. code-block:: javascript
db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102 } } } )
The operation returns the following documents:
.. code-block:: javascript
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
- For the document with ``_id`` equal to ``1``, the ``students``
array contains multiple elements with the ``school`` field
equal to ``102``. However, the :projection:`$elemMatch`
projection returns only the first matching element from the
array.
- The document with ``_id`` equal to ``3`` does not contain the
``students`` field in the result since no element in its
``students`` array matched the :projection:`$elemMatch`
condition.
:projection:`$elemMatch` with Multiple Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :projection:`$elemMatch` projection can specify criteria on multiple
fields:
The following :method:`~db.collection.find()` operation
queries for all documents where the value of the ``zipcode``
field is ``63109``. The projection includes the **first**
matching element of the ``students`` array where the ``school``
field has a value of ``102`` **and** the ``age`` field is greater
than ``10``:
.. code-block:: javascript
db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
The operation returns the three documents that have ``zipcode`` equal to ``63109``:
.. code-block:: javascript
{ "_id" : 1, "students" : [ { "name" : "jess", "school" : 102, "age" : 11 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "ruth", "school" : 102, "age" : 16 } ] }
The document with ``_id`` equal to ``3`` does not contain the ``students`` field
since no array element matched the :projection:`$elemMatch` criteria.
.. seealso::
:projection:`$ (projection) <$>` operator