C# MongoDB missing update aggregations pipline in strong type

Hi i want do update request like:

db.t1.update({ _id: ObjectId("5ec26497deb9782b501b3b7b") }, 
[

    { $addFields : { NextValue: { $sum: ['$seq',1] }} },
    { $addFields : { Obj: { name: "hi2", val: "$NextValue"  } } }
])

I use lastest mongodb and .net mongodb driver.

Compatibility:

enter image description here

In changelog:

Update specification using an aggregation framework pipeline

But i miss set\addFields method, what am I doing wrong ? enter image description here

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

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.