update the data type of field for all documents using the mongodb c driver

This code example works:

var filter = new BsonDocument("MyField", new BsonDocument("$type", BsonType.String));
var stage = new BsonDocument { { "$set", new BsonDocument { { "MyField", new BsonDocument { { "$toObjectId", "$MyField" } } } } } };
var pipeline = PipelineDefinition<BsonDocument, BsonDocument>.Create(stage);
var update = Builders<BsonDocument>.Update.Pipeline(pipeline);

var result = await collection.UpdateManyAsync(filter, update);

Many thanks @kanils_ you pointed me in the right direction, this example Mongo Db driver C# aggregation for update also helped me to write the code above. Also many thanks @dododo for your suggestion.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top