Professional Documents
Culture Documents
Linq Aggregation Framework: Class
Linq Aggregation Framework: Class
For the rest of this page, we’ll use the following class:
class Person
class Pet
Queryable
Hooking into the LINQ provider requires getting access to an IQueryable instance. The
driver provides an AsQueryable extension method on IMongoCollection.
Stages
We’ll walk through the supported stages below:
$project
To generate a $project stage, use the Select method. To see the list of expressions
supported in the Select method, see Aggregation Projections.
// or
NOTE
When projecting scalars, the driver will wrap the scalar into a document with a generated
field name because MongoDB requires that output from an aggregation pipeline be
documents.
var query = from p in collection.AsQueryable()
select p.Name;
The driver will know how to read the field out and transform the results properly.
NOTE
By default, MongoDB will include the _id field in the output unless explicitly excluded. The
driver will automatically add this exclusion when necessary.
$match
The Where method is used to generate a $match stage. To see the list of expressions
supported inside a Where, see Filters.
var query = from p in collection.AsQueryable()
select p;
// or
$redact
The $redact stage is not currently supported using LINQ.
$limit
The Take method is used to generate a $limit stage.
{ $limit: 10 }
$skip
The Skip method is used to generate a $skip stage.
{ $skip: 10 }
]
$unwind
The SelectMany method is used to generate an $unwind stage. In addition, because of
how $unwind works, a $project stage will also be rendered. To see the list of expressions
supported in the SelectMany method, see Aggregation Projections.
select pet;
// or
{ $unwind: "$Pets" }
// or
{ $unwind: "$Pets" }
$group
The GroupBy method is used to generate a $group stage. In general, the GroupBy method
will be followed by the Select containing the accumulators although that isn’t required. To
see the list of supported accumulators, see Accumulators.
//or
$sort
The OrderBy and ThenBy methods are used to generate a $sort stage.
select p;
//or
$geoNear
The $geoNear stage is not currently supported using LINQ.
$out
The $out stage is not currently supported using LINQ.
$lookup
The GroupJoin method is used to generate a $lookup stage.
This operator can take on many forms, many of which are not supported by MongoDB.
Therefore, only 2 forms are supported.
First, you may project into most anything as long as it is supported by the $project operator
and you do not project the original collection variable. Below is an example of a valid query:
Second, you may project into a type with two constructor parameters, the first being the
collection variable and the second being the joined variable. Below is an example of this:
.. note:: An anonymous type, as above, has a constructor with two parameters as required.
Sometimes, the compiler will also generate this two-parameter anonymous type transparently.
Below is an example of this with a custom projection:
{ $unwind: "$joined" },
Supported Methods
The method examples are shown in isolation, but they can be used and combined with all the
other methods as well. You can view the tests for each of these methods in
the MongoQueryableTests.
Any
All forms of Any are supported.
{ $limit: 1 }
{ $limit: 1 }
NOTE
Any has a boolean return type. Since MongoDB doesn’t support this, the driver will pull back
at most 1 document. If one document was retrieved, then the result is true. Otherwise, it’s
false.
Average
All forms of Average are supported.
var result = collection.AsQueryable().Average(p => p.Age);
// or
// or
// or
Distinct
Distinct without an equality comparer is supported.
.Distinct();
// or
var result = collection.AsQueryable().FirstOrDefault();
{ $limit: 1 }
// or
{ $limit: 1 }
GroupBy
See $group.
GroupJoin
See $lookup.
Max
All forms of Max are supported.
// or
var result = collection.AsQueryable().Select(p => p.Age).Max();
Sum
All forms of Sum are supported.
// or
OfType
All forms of OfType are supported.
NOTE
Based on configuration, the discriminator name _t may be different as well as the
value "Customer".
SelectMany
See $unwind.
// or
{ $limit: 2 }
// or
{ $limit: 2 }
]
NOTE
The limit here is 2 because the behavior of Single is to throw when there is more than 1
result. Therefore, we pull back at most 2 documents and throw when 2 documents were
retrieved. If this is not the behavior you wish, then First is the other choice.
GroupBy
See $group.
GroupJoin
See $lookup.
Max
All forms of Max are supported.
// or
Sum
All forms of Sum are supported.
// or
[
{ $group: { _id: 1, __result: { $Sum: "$Age" } } }
OfType
All forms of OfType are supported.
NOTE
Based on configuration, the discriminator name _t may be different as well as the
value "Customer".
Select
See $project.
SelectMany
See $unwind.
// or
[
{ $limit: 2 }
// or
{ $limit: 2 }
NOTE
The limit here is 2 because the behavior of Single is to throw when there is more than 1
result. Therefore, we pull back at most 2 documents and throw when 2 documents were
retrieved. If this is not the behavior you wish, then First is the other choice.
Skip
See $skip.
Sum
All forms of Sum are supported.
// or
[
{ $group: { _id: 1, __result: { $sum: "$Age" } } }
Take
See $limit.
Where
See $match.