我的模型带有嵌入式服务列表:
public class Project
{
public ObjectId Id { get; set; }
public List<Service> Services { get; set; }
}
和
public class Service
{
public int Id { get; set; }
public MachineInfo Info { get; set; }
}
我想将与projectId和serviceId匹配的Info属性修改为列表中的项目。 对于老司机,它是:
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);
但是使用新的驱动程序,我无法使用位置运算符&#39; $&#39;:
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);
任何想法怎么办?使用旧版驱动程序?
答案 0 :(得分:6)
你可以在这里做两件事......要么像在1.x中那样使用字符串......
Builders<Project>.Update.Set("Services.$.Info", newInfo);
或使用ElementAt(-1)
Builders<Project>.Update.Set(x => x.Services.ElementAt(-1).Info, newInfo);