Aggregation Operations
On this page
Overview
In this guide, you can learn how to use the MongoDB .NET/C# Driver to perform aggregation operations.
Aggregation operations process data in your MongoDB collections and return computed results. The MongoDB Aggregation framework is modeled on the concept of data processing pipelines. Documents enter a pipeline comprised of one or more stages, and this pipeline transforms the documents into an aggregated result.
To learn more about the aggregation stages supported by the .NET/C# Driver, see Aggregation Stages.
Analogy
Aggregation operations function similarly to car factories with assembly lines. The assembly lines have stations with specialized tools to perform specific tasks. For example, when building a car, the assembly line begins with the frame. Then, as the car frame moves through the assembly line, each station assembles a separate part. The result is a transformed final product, the finished car.
The assembly line represents the aggregation pipeline, the individual stations represent the aggregation stages, the specialized tools represent the expression operators, and the finished product represents the aggregated result.
Compare Aggregation and Find Operations
The following table lists the different tasks you can perform with find operations, compared to what you can achieve with aggregation operations. The aggregation framework provides expanded functionality that allows you to transform and manipulate your data.
Find Operations | Aggregation Operations |
---|---|
Select certain documents to return Select which fields to return Sort the results Limit the results Count the results | Select certain documents to return Select which fields to return Sort the results Limit the results Count the results Group the results Rename fields Compute new fields Summarize data Connect and merge data sets |
Server Limitations
Consider the following limitations when performing aggregation operations:
Returned documents must not violate the BSON document size limit of 16 megabytes.
Pipeline stages have a memory limit of 100 megabytes by default. If required, you can exceed this limit by setting the AllowDiskUse property of the
AggregateOptions
object that you pass to theAggregate()
method.
Troubleshooting
Unsupported filter
or Expression not supported
If you use a LINQ or builder expression that isn't available in the Query API,
you might receive an Unsupported filter ...
or Expression not
supported ...
exception message.
An expression might not be available in the following cases:
You are attempting to use a .NET/C# feature that doesn't have an equivalent MongoDB representation. For example, .NET/C# and MongoDB have different semantics around collations.
The driver doesn't support a particular transformation from the LINQ or builder expression into the Query API. This might happen because the provided query has no Query API translation or because a feature hasn't been implemented in the driver.
If you receive one of these exceptions, try the following steps:
Use the MongoDB C# Analyzer to analyze your expressions.
Simplify your query where possible.
Provide a query as a
BsonDocument
object or JSON string. All definition classes, such asFilterDefinition
,ProjectionDefinition
, andPipelineDefinition
, support implicit conversion fromBsonDocument
objects or JSON strings. For example, the following filters are equivalent when used in a query or aggregation:FilterDefinition<Entity> typedFilter = Builders<Entity>.Filter.Eq(e => e.A, 1); FilterDefinition<Entity> bsonFilter = new BsonDocument {{ "a", 1 }}; FilterDefinition<Entity> jsonFilter = "{ a : 1 }";
You can combine BsonDocument
objects, JSON strings, POCOs in the same
query, as shown in the following example:
FilterDefinition<Entity> filter = Builders<Entity>.Filter .And(Builders<Entity>.Filter .Eq(e => e.A, 1), BsonDocument .Parse("{ b : 2 }"));
Note
If you use a BsonDocument
object or JSON string, your field names must match
the case-sensitive names stored by the server. For example, when referencing
the _id
field, you must refer to it by using the field name _id
.
Because the Query API doesn't recognize
manual class mappings,
BSON serialization attributes, or serialization conventions, you can't use these
mechanisms to change field names. For example, if a document contains a field named
FirstName
annotated with [BsonElement("first_name")]
, you must refer to it
as first_name
in BsonDocument
or JSON string definitions.
Additional Information
To view a full list of expression operators, see Aggregation Operators.
To learn about explaining MongoDB aggregation operations, see Explain Results and Query Plans.