Skip to content

Commit 6d8875a

Browse files
DOCSP-47510 Add new aggregation operation examples (#55)
1 parent 41f8154 commit 6d8875a

File tree

3 files changed

+170
-14
lines changed

3 files changed

+170
-14
lines changed

source/fundamentals/query-data.txt

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,124 @@ on the ``name`` field.
169169
When sorting on fields with a boolean value, entities with a field value of
170170
``false`` show before those with a value of ``true``.
171171

172+
Scalar Aggregations
173+
-------------------
174+
175+
The {+provider-short+} supports the following scalar aggregation methods:
176+
177+
- ``Count()``: Returns the number of elements in a collection or the number of
178+
documents that match a predicate
179+
- ``LongCount()``: Returns the number of elements in a collection as a ``long``
180+
or the number of documents that match a predicate
181+
- ``Any()``: Returns ``true`` if any elements in a collection match the predicate
182+
- ``Max()``: Returns the maximum value of a specified field in a collection
183+
- ``Min()``: Returns the minimum value of a specified field in a collection
184+
- ``Sum()``: Returns the sum of values of a specified field in a collection
185+
- ``Average()``: Returns the average of values of a specified field in a collection
186+
187+
The following sections show examples of each of the preceding methods.
188+
189+
Count
190+
~~~~~
191+
192+
The following example uses the ``Count()`` method to count the number of elements in the
193+
``Planets`` collection:
194+
195+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
196+
:language: csharp
197+
:start-after: // start-count
198+
:end-before: // end-count
199+
200+
The following example uses the ``Count()`` method to count the number of elements in the
201+
``Planets`` collection that have a ``hasRings`` field set to ``true``:
202+
203+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
204+
:language: csharp
205+
:start-after: // start-count-predicate
206+
:end-before: // end-count-predicate
207+
208+
LongCount
209+
~~~~~~~~~
210+
211+
The following example uses the ``LongCount()`` method to count the number of
212+
elements in the ``Planets`` collection and returns the result as a ``long``:
213+
214+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
215+
:language: csharp
216+
:start-after: // start-long-count
217+
:end-before: // end-long-count
218+
219+
The following example uses the ``LongCount()`` method to count the number of
220+
elements in the ``Planets`` collection that have a ``hasRings`` field set to
221+
``true`` and returns the result as a ``long``:
222+
223+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
224+
:language: csharp
225+
:start-after: // start-long-count-predicate
226+
:end-before: // end-long-count-predicate
227+
228+
Any
229+
~~~
230+
231+
The following example uses the ``Any()`` method to check if any elements in the
232+
``Planets`` collection have a ``hasRings`` field set to ``true``:
233+
234+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
235+
:language: csharp
236+
:start-after: // start-any
237+
:end-before: // end-any
238+
239+
Max
240+
~~~
241+
242+
The following example uses the ``Max()`` method to find the maximum value of the
243+
``orderFromSun`` field in the ``Planets`` collection:
244+
245+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
246+
:language: csharp
247+
:start-after: // start-max
248+
:end-before: // end-max
249+
250+
Min
251+
~~~
252+
253+
The following example uses the ``Min()`` method to find the minimum value of the
254+
``orderFromSun`` field in the ``Planets`` collection:
255+
256+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
257+
:language: csharp
258+
:start-after: // start-min
259+
:end-before: // end-min
260+
261+
Sum
262+
~~~
263+
264+
The following example uses the ``Sum()`` method to find the sum of the
265+
``mass`` field in the ``Planets`` collection:
266+
267+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
268+
:language: csharp
269+
:start-after: // start-sum
270+
:end-before: // end-sum
271+
272+
Average
273+
~~~~~~~
274+
275+
The following example uses the ``Average()`` method to find the average value of
276+
the ``mass`` field in the ``Planets`` collection:
277+
278+
.. literalinclude:: /includes/fundamentals/code-examples/aggregation.cs
279+
:language: csharp
280+
:start-after: // start-average
281+
:end-before: // end-average
282+
283+
172284
Additional Information
173285
----------------------
174286

287+
To learn more about aggregations in MongoDB, see the :manual:`Aggregation
288+
Operations </aggregation>` guide in the {+mdb-server+} manual.
289+
175290
To learn more about the methods discussed in this guide, see the following .NET API
176291
documentation links:
177292

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// start-count
2+
var planetCount = db.Planets.Count();
3+
4+
Console.WriteLine("Planet Count: " + planetCount);
5+
// end-count
6+
7+
// start-count-predicate
8+
var planetCountWithRings = db.Planets.Count(p => p.hasRings);
9+
10+
Console.WriteLine("Planet Count with Rings: " + planetCountWithRings);
11+
// end-count-predicate
12+
13+
// start-long-count
14+
var planetCountLong = db.Planets.LongCount();
15+
16+
Console.WriteLine("Long Planet Count: " + longCount);
17+
// end-long-count
18+
19+
// start-long-count-predicate
20+
var planetCountLongWithRings = db.Planets.LongCount(p => p.hasRings);
21+
22+
Console.WriteLine("Long Planet Count with Rings: " + planetCountLongWithRings);
23+
// end-long-count-predicate
24+
25+
// start-any
26+
var results = db.Planets.Any(p => p.hasRings);
27+
28+
foreach (var p in results)
29+
{
30+
Console.WriteLine("Planet with Rings: " + p.name);
31+
}
32+
// end-any
33+
34+
// start-max
35+
var furthestPlanet = db.Planets.Max(p => p.orderFromSun);
36+
37+
Console.WriteLine("Furthest Planet: " + furthestPlanet.name);
38+
// end-max
39+
40+
// start-min
41+
var closestPlanet = db.Planets.Min(p => p.OrderFromSun);
42+
43+
Console.WriteLine("Closest Planet: " + closestPlanet.Name);
44+
// end-min
45+
46+
// start-sum
47+
var totalMass = db.Planets.Sum(p => p.mass);
48+
Console.WriteLine("Total Mass of Planets: " + totalMass);
49+
// end-sum
50+
51+
// start-average
52+
var averageMass = db.Planets.Average(p => p.mass);
53+
54+
Console.WriteLine("Average Mass of Planets: " + averageMass);
55+
// end-average

source/limitations.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ types not recognized by {+framework+}.
4343
This version of the {+provider-short+} does not support
4444
Select Projections.
4545

46-
Scalar Aggregations
47-
~~~~~~~~~~~~~~~~~~~
48-
49-
Top-level scalar aggregations are operations you can perform on a query, such as
50-
``Count()``, ``Min()``, and ``Max()``. This version of the {+provider-short+}
51-
supports only the following scalar aggregation operations:
52-
53-
- ``Count()``
54-
- ``LongCount()``
55-
- ``Any()``, with or without predicates
56-
57-
This version of the {+provider-short+} does not support other scalar aggregation
58-
operations.
59-
6046
Migrations
6147
~~~~~~~~~~
6248

0 commit comments

Comments
 (0)