How to use $push update modifier in MongoDB and C#, when updating an array in a document How to use $push update modifier in MongoDB and C#, when updating an array in a document mongodb mongodb

How to use $push update modifier in MongoDB and C#, when updating an array in a document


it should be something like this:

unicorns.Update(Query.EQ("name", "Aurora"), Update.Push("loves", "sugar"));


I would like to also illustrate how to do it using a different syntax

var filter = Builders<Unicorn>             .Filter.Eq(e => e.Name, "Aurora");var update = Builders<Unicorn>.Update        .Push<String>(e => e.Likes, like);await fantasyContext.Unicorns.FindOneAndUpdateAsync(filter, update);


To do this with the updated syntax and regular BsonDocuments instead of defined objects, use the following:

var filter = Builders<BsonDocument>.Filter.Eq("name": "Aurora");var update = Builders<BsonDocument>.Update.Push("loves", "sugar"):// you can also use the async update method from Alex's answer herevar result = fantasyContext.Unicorns.UpdateOne(filter, update);