Unwind then Group aggregation in MongoDB C#

Unwind then Group aggregation in MongoDB C#

In MongoDB C#, you can perform an unwind and group aggregation using the Aggregate method and the Builders class to construct the pipeline.

Here's an example of how to unwind and group a collection by a specific field:

var collection = _database.GetCollection<BsonDocument>("myCollection"); var pipeline = Builders<BsonDocument>.Pipeline .Unwind("myArrayField") .Group(new BsonDocument { { "_id", "$myGroupField" }, { "count", new BsonDocument("$sum", 1) } }); var result = collection.Aggregate(pipeline).ToList(); 

In this example, we first get a reference to the collection we want to aggregate. We then use the Builders class to create a pipeline that performs an unwind operation on the myArrayField field, which contains an array of values. This creates a new document for each value in the array.

We then perform a group operation on the resulting documents, grouping them by the myGroupField field and counting the number of documents in each group using the $sum operator.

Finally, we execute the aggregation using the Aggregate method and store the results in a list.

Note that you can modify the pipeline to suit your specific needs, including adding additional stages such as $match, $sort, and $project. Also, be sure to use the appropriate types for your collection and data.

Examples

  1. "MongoDB C# unwind then group example"

    • Description: Learn how to use the $unwind and $group stages in MongoDB with C# to flatten arrays and group documents based on specific fields.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "totalCount", new BsonDocument("$sum", 1) } }) .ToList(); 
  2. "C# MongoDB aggregate unwind group sort"

    • Description: Explore a MongoDB C# example that combines $unwind, $group, and $sort stages to unwind arrays, group documents, and sort the result.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "totalCount", new BsonDocument("$sum", 1) } }) .Sort(new BsonDocument("totalCount", -1)) .ToList(); 
  3. "MongoDB C# aggregate unwind group with project"

    • Description: Implement a MongoDB C# aggregation query that combines $unwind, $group, and $project stages to unwind arrays, group documents, and project specific fields.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "totalCount", new BsonDocument("$sum", 1) } }) .Project(new BsonDocument { { "_id", 0 }, { "groupField", "$_id" }, { "totalCount", 1 } }) .ToList(); 
  4. "C# MongoDB aggregate unwind group with average"

    • Description: Discover how to use $unwind, $group, and calculate the average value during aggregation in MongoDB with C#.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "averageValue", new BsonDocument("$avg", "$numericField") } }) .ToList(); 
  5. "C# MongoDB aggregate multiple unwind group stages"

    • Description: Explore a MongoDB C# example that involves multiple $unwind and $group stages in the aggregation pipeline to handle nested arrays.
    • Code:
      var result = collection.Aggregate() .Unwind("firstNestedArrayField") .Unwind("secondNestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "totalCount", new BsonDocument("$sum", 1) } }) .ToList(); 
  6. "MongoDB C# aggregate unwind group with max value"

    • Description: Learn how to use $unwind, $group, and find the maximum value during aggregation in MongoDB with C#.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "maxValue", new BsonDocument("$max", "$numericField") } }) .ToList(); 
  7. "C# MongoDB aggregate unwind group with filtering"

    • Description: Implement a MongoDB C# aggregation query that combines $unwind, $group, and $match stages to filter documents before grouping.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Match(new BsonDocument("filterField", "desiredValue")) .Group(new BsonDocument { { "_id", "$groupField" }, { "totalCount", new BsonDocument("$sum", 1) } }) .ToList(); 
  8. "MongoDB C# aggregate unwind group with push array"

    • Description: Discover how to use $unwind, $group, and $push to create an array during aggregation in MongoDB with C#.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "dataArray", new BsonDocument("$push", "$arrayField") } }) .ToList(); 
  9. "C# MongoDB aggregate unwind group with conditional count"

    • Description: Explore a MongoDB C# example that uses $unwind, $group, and a conditional $sum to count specific occurrences during aggregation.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "conditionalCount", new BsonDocument("$sum", new BsonDocument("$cond", new BsonArray { new BsonDocument("$eq", new BsonArray { "$conditionField", "desiredValue" }), 1, 0 })) } }) .ToList(); 
  10. "C# MongoDB aggregate unwind group with weighted average"

    • Description: Learn how to use $unwind, $group, and calculate a weighted average during aggregation in MongoDB with C#.
    • Code:
      var result = collection.Aggregate() .Unwind("nestedArrayField") .Group(new BsonDocument { { "_id", "$groupField" }, { "weightedAverage", new BsonDocument("$avg", new BsonDocument("$multiply", new BsonArray { "$numericField", "$weightField" })) } }) .ToList(); 

More Tags

stackdriver jquery-1.3.2 screen-orientation touches belongs-to behaviorsubject reactor crc shopify ssis

More C# Questions

More Date and Time Calculators

More Weather Calculators

More Organic chemistry Calculators

More Fitness-Health Calculators