C# MongoDB missing update aggregations pipline in strong type

Hi @alexov_inbox,

As of MongoDB C# driver version 2.10, there is no strong typed definition for $addFields aggregation pipeline stage. However you can still construct BsonDocument to build a pipeline definition stage. For example:

var pipeline = new BsonDocumentStagePipelineDefinition<BsonDocument, BsonDocument>(
                 new[] { 
                   new BsonDocument{{"$addFields", 
                     new BsonDocument{{"NextValue", 
                       new BsonDocument{{ "$sum", new BsonArray().Add("$seq").Add(1) } } 
                     }}
                   }},
                   new BsonDocument{{"$addFields", 
                     new BsonDocument{{"Obj", 
                       new BsonDocument("name", "hi2").Add("val", "$NextValue")
                     }}
                   }} 
                 } 
);
var updateDefinition = new PipelineUpdateDefinition<BsonDocument>(pipeline);
var result = collection.UpdateOne(new BsonDocument{}, updateDefinition);   

What you see in the release change is likely related to CSHARP-2570, which is to support aggregation pipeline definition on an update operation.

In addition, without knowing more the context of you aggregation pipeline, you may be able to replace $sum with $inc. As it looks like you’re just incrementing the value by one.

Regards,
Wan.

2 Likes