Skip to content

Commit 7eee38c

Browse files
authored
DOCSP-49771 - Add $elemMatch C# examples (#11912)
1 parent b95614a commit 7eee38c

File tree

4 files changed

+240
-47
lines changed

4 files changed

+240
-47
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Driver;
3+
4+
namespace Projection;
5+
6+
public class ElemMatchExamples
7+
{
8+
static string _uri = Environment.GetEnvironmentVariable("MONGODB_URI");
9+
10+
static IMongoCollection<School> schoolsCollection = new MongoClient(_uri)
11+
.GetDatabase("example")
12+
.GetCollection<School>("schools");
13+
14+
public static List<BsonDocument> ZipSearch()
15+
{
16+
// start zipSearch
17+
var results = schoolsCollection
18+
.Find(s => s.ZipCode == "63109")
19+
.Project(Builders<School>.Projection.ElemMatch(
20+
field: school => school.Students,
21+
filter: student => student.School == 102
22+
)
23+
).ToList();
24+
// end zipSearch
25+
26+
return results;
27+
}
28+
29+
public static List<BsonDocument> ZipMultipleSearch()
30+
{
31+
// start zipMultipleSearch
32+
var results = schoolsCollection
33+
.Find(s => s.ZipCode == "63109")
34+
.Project(Builders<School>.Projection.ElemMatch(
35+
field: school => school.Students,
36+
filter: student => (student.School == 102) && (student.Age > 10)
37+
)
38+
).ToList();
39+
// end zipMultipleSearch
40+
41+
return results;
42+
}
43+
44+
public static List<BsonDocument> ZipAthleticsIncomplete()
45+
{
46+
// start zipAthleticsIncomplete
47+
var results = schoolsCollection
48+
.Find(s => s.ZipCode == "63109")
49+
.Project(Builders<School>.Projection.ElemMatch(
50+
"athletics",
51+
Builders<School>.Filter.Eq("athletics", "basketball"))
52+
).ToList();
53+
// end zipAthleticsIncomplete
54+
55+
return results;
56+
}
57+
58+
public static List<BsonDocument> ZipAthletics()
59+
{
60+
// start zipAthletics
61+
var results = schoolsCollection
62+
.Find(s => s.ZipCode == "63109")
63+
.Project(Builders<School>.Projection.ElemMatch(
64+
field: "athletics",
65+
filter: Builders<School>.Filter.Eq("$eq", "basketball"))
66+
).ToList();
67+
// end zipAthletics
68+
69+
return results;
70+
}
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class School
2+
{
3+
public string Id { get; set; }
4+
5+
[BsonElement("zipcode")]
6+
public string ZipCode { get; set; }
7+
8+
public Student[] Students { get; set; }
9+
10+
public string[] Athletics { get; set; }
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Student
2+
{
3+
public string Id { get; set; }
4+
5+
public string Name { get; set; }
6+
7+
public int School { get; set; }
8+
9+
public int Age { get; set; }
10+
}

content/manual/upcoming/source/reference/operator/projection/elemMatch.txt

Lines changed: 148 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Restrictions
4747
- You cannot specify a :query:`$text` query operator in an
4848
:projection:`$elemMatch`.
4949

50-
5150
Examples
5251
--------
5352

@@ -57,8 +56,7 @@ assumes a collection ``schools`` with the following documents:
5756
.. code-block:: javascript
5857
:copyable: true
5958

60-
db.school.insertMany( [
61-
{
59+
{
6260
_id: 1,
6361
zipcode: "63109",
6462
students: [
@@ -93,30 +91,72 @@ assumes a collection ``schools`` with the following documents:
9391
{ name: "ruth", school: 102, age: 16 },
9492
]
9593
}
96-
] )
9794

95+
.. tabs-drivers::
96+
97+
.. tab:: MongoDB Shell
98+
:tabid: shell
99+
100+
.. tab:: C#
101+
:tabid: csharp
102+
103+
You can model these documents by using the following C# classes:
98104

105+
.. literalinclude:: /includes/driver-examples/projection/School.cs
106+
:language: csharp
107+
108+
.. literalinclude:: /includes/driver-examples/projection/Student.cs
109+
:language: csharp
99110

100111
Zipcode Search
101112
~~~~~~~~~~~~~~
102113

103-
The following :method:`~db.collection.find()` operation
104-
queries for all documents where the value of the ``zipcode``
105-
field is ``63109``. The :projection:`$elemMatch` projection
106-
returns only the **first** matching element of the ``students``
107-
array where the ``school`` field has a value of ``102``:
114+
.. tabs-selector:: drivers
108115

109-
.. code-block:: javascript
110-
:copyable: true
116+
.. tabs-drivers::
117+
118+
.. tab:: MongoDB Shell
119+
:tabid: shell
111120

112-
db.schools.find( { zipcode: "63109" },
113-
{ students: { $elemMatch: { school: 102 } } } )
121+
The following :method:`~db.collection.find()` operation
122+
queries for all documents where the value of the ``zipcode``
123+
field is ``"63109"``. The :projection:`$elemMatch` projection
124+
returns only the **first** matching element of the ``students``
125+
array where the ``school`` field has a value of ``102``:
114126

115-
The operation returns the following documents that have ``zipcode``
116-
equal to ``63109`` and projects the ``students`` array using
127+
.. code-block:: javascript
128+
:copyable: true
129+
130+
db.schools.find( { zipcode: "63109" },
131+
{ students: { $elemMatch: { school: 102 } } } )
132+
133+
.. tab:: C#
134+
:tabid: csharp
135+
136+
To perform an ``$elemMatch`` projection when using the .NET/C# driver, call the
137+
``ElemMatch()`` method on the projection builder. Pass the name of the array field
138+
to project and the filter to apply to the array elements.
139+
140+
The following code example finds all documents in which the value of the ``Zipcode``
141+
field is ``"63109"``. For each matching document, the projection returns the
142+
following fields:
143+
144+
- ``Id``
145+
- The first element of the ``Students`` array in which the value of the nested
146+
``School`` field has the value ``102``
147+
148+
.. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
149+
:language: csharp
150+
:start-after: // start zipSearch
151+
:end-before: // end zipSearch
152+
:dedent: 8
153+
154+
The operation returns the following documents that have a ``zipcode`` value of
155+
``"63109"`` and projects the ``students`` array using
117156
:projection:`$elemMatch`:
118157

119158
.. code-block:: javascript
159+
:copyable: false
120160

121161
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
122162
{ "_id" : 3 }
@@ -137,24 +177,47 @@ equal to ``63109`` and projects the ``students`` array using
137177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138178

139179
The :projection:`$elemMatch` projection can specify criteria on multiple
140-
fields:
180+
fields.
141181

142-
The following :method:`~db.collection.find()` operation
143-
queries for all documents where the value of the ``zipcode``
144-
field is ``63109``. The projection includes the **first**
145-
matching element of the ``students`` array where the ``school``
146-
field has a value of ``102`` **and** the ``age`` field is greater
147-
than ``10``:
182+
.. tabs-drivers::
148183

149-
.. code-block:: javascript
150-
:copyable: true
184+
.. tab:: MongoDB Shell
185+
:tabid: shell
186+
187+
The following :method:`~db.collection.find()` operation
188+
queries for all documents where the value of the ``zipcode``
189+
field is ``"63109"``. The projection includes the **first**
190+
matching element of the ``students`` array where the ``school``
191+
field has a value of ``102`` **and** the ``age`` field is greater
192+
than ``10``:
151193

152-
db.schools.find( { zipcode: "63109" },
153-
{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
194+
.. code-block:: javascript
195+
:copyable: true
154196

155-
The operation returns the three documents that have ``zipcode`` equal to ``63109``:
197+
db.schools.find( { zipcode: "63109" },
198+
{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
199+
200+
.. tab:: C#
201+
:tabid: csharp
202+
203+
The following code example finds all documents in which the value of the ``Zipcode``
204+
field is ``"63109"``. For each matching document, the projection returns the following fields:
205+
206+
- ``Id``
207+
- The first element of the ``Students`` array in which the value of the nested
208+
``School`` field has the value ``102`` and the ``Age`` field has a value greater than
209+
``10``
210+
211+
.. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
212+
:language: csharp
213+
:start-after: // start zipMultipleSearch
214+
:end-before: // end zipMultipleSearch
215+
:dedent: 8
216+
217+
The operation returns the three documents that have a ``zipcode`` value of ``"63109"``:
156218

157219
.. code-block:: javascript
220+
:copyable: false
158221

159222
{ "_id" : 1, "students" : [ { "name" : "jess", "school" : 102, "age" : 11 } ] }
160223
{ "_id" : 3 }
@@ -164,43 +227,81 @@ The document with ``_id`` equal to ``3`` does not contain the ``students`` field
164227
since no array element matched the :projection:`$elemMatch` criteria.
165228

166229
The argument to :projection:`$elemMatch` matches elements of the array that
167-
``$elemMatch`` is projecting. If you specify an equality with a field
230+
``$elemMatch`` is projecting. If you specify an equality with a field
168231
name to ``$elemMatch``, it attempts to match objects within the array.
169232
For example, ``$elemMatch`` attempts to match objects, instead of scalar
170233
values, within the array for the following in the projection:
171234

172-
.. code-block:: javascript
173-
:copyable: true
235+
.. tabs-drivers::
174236

175-
db.schools.find( { zipcode: "63109" },
176-
{ athletics: { $elemMatch: { athletics: "basketball" } } })
237+
.. tab:: MongoDB Shell
238+
:tabid: shell
177239

178-
To match scalar values, use the equality operator along with the scalar
179-
value that you want to match (``{$eq: <scalar value>}``). For example,
180-
the following :method:`~db.collection.find()` operation queries for all
181-
documents where the value of the ``zipcode`` field is ``63109``. The
182-
projection includes the matching element of the ``athletics`` array
183-
where the value is ``basketball``:
240+
.. code-block:: javascript
184241

185-
.. code-block:: javascript
186-
:copyable: true
242+
db.schools.find( { zipcode: "63109" },
243+
{ athletics: { $elemMatch: { athletics: "basketball" } } })
244+
245+
.. tab:: C#
246+
:tabid: csharp
247+
248+
.. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
249+
:language: csharp
250+
:start-after: // start zipAthleticsIncomplete
251+
:end-before: // end zipAthleticsIncomplete
252+
:dedent: 8
253+
254+
The preceding examples return the documents that have a ``zipcode`` value of
255+
``"63109"``, but these documents include only the ``_id`` field because the projection
256+
operation found no matching elements.
257+
258+
.. tabs-drivers::
259+
260+
.. tab:: MongoDB Shell
261+
:tabid: shell
262+
263+
To match scalar values, use the equality operator along with the scalar
264+
value that you want to match (``{$eq: <scalar value>}``). For example,
265+
the following :method:`~db.collection.find()` operation queries for all
266+
documents where the value of the ``zipcode`` field is ``"63109"``. The
267+
projection includes the matching element of the ``athletics`` array
268+
where the value is ``basketball``:
269+
270+
.. code-block:: javascript
271+
:copyable: true
272+
273+
db.schools.find( { zipcode: "63109" },
274+
{ athletics: { $elemMatch: { $eq: "basketball" } } })
275+
276+
.. tab:: C#
277+
:tabid: csharp
278+
279+
To perform an ``$elemMatch`` operation against scalar values in an array when using
280+
the .NET/C# driver, call the ``ElemMatch()`` method
281+
on the projection builder. Pass the name of the array field to project and an
282+
equality filter for the field ``"$eq"`` and the value you want to compare against.
187283

188-
db.schools.find( { zipcode: "63109" },
189-
{ athletics: { $elemMatch: { $eq: "basketball" } } })
284+
.. literalinclude:: /includes/driver-examples/projection/ElemMatchExamples.cs
285+
:language: csharp
286+
:start-after: // start zipAthletics
287+
:end-before: // end zipAthletics
288+
:dedent: 8
190289

191-
The operation returns the three documents that have ``zipcode`` equal to
192-
``63109``:
290+
The operation returns the three documents that have ``zipcode`` value of
291+
``"63109"``. The returned documents include the ``_id`` field and matching
292+
elements of the ``athletics`` array, if any.
193293

194294
.. code-block:: javascript
295+
:copyable: false
195296

196297
[
197-
{ _id : 1 },
298+
{ _id: 1 },
198299
{ _id: 3, athletics: [ 'basketball' ] },
199-
{ _id : 4 }
300+
{ _id: 4 }
200301
]
201302

202303
The document with ``_id`` equal to ``3`` is the only document that
203-
matched the :projection:`$elemMatch` criteria.
304+
matched the :projection:`$elemMatch` criteria.
204305

205306
.. seealso::
206307

0 commit comments

Comments
 (0)