I've run the following code in mongo shell:
db.unicorns.insert({name: 'Dunx', loves: ['grape', 'watermelon']});
and now I've something like this in my MongoDB collection:
{name: 'Dunx', loves: ['grape', 'watermelon']}
As you can see loves
is an array.
Question
How can I write C# code, with the official C# driver, that does the following:
db.unicorns.update({name: 'Aurora'}, {$push: {loves: 'sugar'}})
The above code runs just fine in mongo shell.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With