Mongo Db driver C# aggregation for update

Viewed 728

How to create a new field (String) from value of another existing field (NubmerLong) for all documents in the collection via C# driver? There is no problem using Mong Shell:

db.getCollection("MyCollection").updateMany(
  { },
  [{ $set: { recordId: { $toString: "$id" } } }]
)

Mongo driver has UpdateDefinitions and has PipelineDefinitions. And no compatibility between them.

1 Answers

Here's a way to merge approaches Update + Pipeline for C# driver:

var stage = new BsonDocument { { "$set", new BsonDocument { { "recordId", new BsonDocument { { "$toString", "$id" } } } } } };
var pipeline = PipelineDefinition<BsonDocument, BsonDocument>.Create(stage);
...
Builders<BsonDocument>.Update.Pipeline(pipeline)
Related