Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

BulkWrite c# example mongodb

//update definitions
var filterDefinition = Builders<User>.Filter.Eq(p => p.isBlocked, false);
var updateDefinition = Builders<User>.Update.Set(p => p.isBlocked, true);

public static async Task BulkUpdateManyMongoDb()
    {
        IMongoDatabase db = _client.GetDatabase("sample_blog");
        var userCollection = db.GetCollection<User>("users");

        var listWrites = new List<WriteModel<User>>();

        var filterDefinition = Builders<User>.Filter.Eq(p => p.isBlocked, false);
        var updateDefinition = Builders<User>.Update.Set(p => p.isBlocked, true);

        listWrites.Add(new UpdateManyModel<User>(filterDefinition, updateDefinition));

        await userCollection.BulkWriteAsync(listWrites);
    }
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #BulkWrite #mongodb
ADD COMMENT
Topic
Name
6+5 =