Search code examples
c#mongodbmongodb-.net-driver

Update an embedded document from a collection using MongoDB and C# new driver version (2.0)


I've a model with an a list of embedded Service:

public class Project
{
    public ObjectId Id { get; set; }
    public List<Service> Services { get; set; }
}

And

public class Service
{
    public int Id { get; set; }
    public MachineInfo Info { get; set; }
}

I want to modify Info property matching a projectId and a serviceId into an item of the list. With old driver, it was:

var result = collection.Update(
        Query.And(
          Query.EQ("_id", projectId),
          Query.ElemMatch("Services", Query.EQ("Id", serviceId))
        ), 
var update = Update.Set("Services.$.Info", newInfo);
Collection.Update(query, update);

But using the new driver, I don't manage to use position operator '$':

var filter = Builders<Project>.Filter.And(Builders<Project>.Filter.Eq(x => x.Id, projectId),
    Builders<Project>.Filter.ElemMatch(x => x.Services, x => x.Id == serviceId));
var update = Builders<Project>.Update.Set(x => x.$.Info, newInfo);
this.collection.UpdateOneAsync(filter, update);

Any idea to how to do ? Use legacy driver ?


Solution

  • You can do 2 things here... Either use a string like you did in 1.x...

    Builders<Project>.Update.Set("Services.$.Info", newInfo);
    

    or use ElementAt(-1)

    Builders<Project>.Update.Set(x => x.Services.ElementAt(-1).Info, newInfo);