Docs Menu
Docs Home
/ / /
C#/.NET

Aggregation Operations

On this page

  • Overview
  • Analogy
  • Compare Aggregation and Find Operations
  • Server Limitations
  • Troubleshooting
  • Unsupported filter or Expression not supported
  • Additional Information

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.

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.

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

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 the Aggregate() method.

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:

  1. 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.

  2. 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:

  1. Use the MongoDB C# Analyzer to analyze your expressions.

  2. Simplify your query where possible.

  3. Provide a query as a BsonDocument object or JSON string. All definition classes, such as FilterDefinition, ProjectionDefinition, and PipelineDefinition, support implicit conversion from BsonDocument 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.

To view a full list of expression operators, see Aggregation Operators.

To learn about explaining MongoDB aggregation operations, see Explain Results and Query Plans.

Back

Search Geospatially