Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo update array element (.NET driver 2.0)

Tags:

c#

.net

mongodb

EDIT: Not looking for the javascript way of doing this. I am looking for the MongoDB C# 2.0 driver way of doing this (I know it might not be possible; but I hope somebody knows a solution).

I am trying to update the value of an item embedded in an array on the primary document in my mongodb.

I am looking for a strongly typed way to do this. I am using the Mongodb c# 2.0 driver

I can do it by popping the element, updating the value, then reinserting. This just doesn't feel right; since I am overwriting what might have been written in the meantime.

Here is what I have tried so far but with no luck:

private readonly IMongoCollection<TempAgenda> _collection;  void Main() {     var collectionName = "Agenda";     var client = new MongoClient("mongodb://localhost:27017");     var db = client.GetDatabase("Test");     _collection = db.GetCollection<TempAgenda>(collectionName);     UpdateItemTitle(1, 1, "hello"); }  public void UpdateItemTitle(string agendaId, string itemId, string title){     var filter = Builders<TempAgenda>.Filter.Eq(x => x.AgendaId, agendaId);     var update = Builders<TempAgenda>.Update.Set(x => x.Items.Single(p => p.Id.Equals(itemId)).Title, title);     var result = _collection.UpdateOneAsync(filter, update).Result; } 
like image 944
Kristian Barrett Avatar asked Jul 16 '15 12:07

Kristian Barrett


People also ask

How do you update an array element in MongoDB?

You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

Can we use array in MongoDB?

Unlike relational database models, MongoDB documents can have fields which have values as arrays. The prototypical example in almost all MongoDB documentation is a document having a tags field, whose value is an array of strings, such as ["NoSQL", "Ruby", "MongoDB"] .

What is $Push in MongoDB?

Description. In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element.


Video Answer


1 Answers

Took me a while to figure this out as it doesn't appear to be mentioned in any of the official documentation (or anywhere else). I did however find this on their issue tracker, which explains how to use the positional operator $ with the C# 2.0 driver.

This should do what you want:

public void UpdateItemTitle(string agendaId, string itemId, string title){     var filter = Builders<TempAgenda>.Filter.Where(x => x.AgendaId == agendaId && x.Items.Any(i => i.Id == itemId));     var update = Builders<TempAgenda>.Update.Set(x => x.Items[-1].Title, title);     var result = _collection.UpdateOneAsync(filter, update).Result; } 

Notice that your Item.Single() clause has been changed to Item.Any() and moved to the filter definition.

[-1] or .ElementAt(-1) is apparently treated specially (actually everything < 0) and will be replaced with the positional operator $.

The above will be translated to this query:

db.Agenda.update({ AgendaId: 1, Items.Id: 1 }, { $set: { Items.$.Title: "hello" } }) 
like image 55
Søren Kruse Avatar answered Sep 22 '22 17:09

Søren Kruse