如何在Mongo文档的子属性中设置整个数组? 这是我当前的文档模型,是一间带房间的房子,每个房间都有家具。
public enum Furnitures{
Table,
Chair
}
public class House{
public string Id {get; set;}
public Room[] Rooms {get; set;}
}
public class Room {
public string Id {get; set;}
public Furniture[] Furnitures {get; set;}
}
这是我的更新方法,但不起作用:
void InteriorDesign( IMongoCollection<House> collection, string houseId, string roomId, Furniture[] newfurnitures)
{
filterBuilder = Builders<House>().Filter;
var filter = filterBuilder.Eq( x => x.Id, houseId) &
filterBuilder.ElemMatch( house => house.Rooms, room => room.Id == roomId);
updateBuilder = Builders<House>.Update;
var update=
// HERE I EXPECT the NEW array replace the old one
updateBuilder.Set( house => house.Rooms[-1].Funitures, newfurnitures);
collection.FindOneAndUpdate(filter,update);
}
如何用另一个全新的阵列替换一个阵列的内容?
答案 0 :(得分:1)
您的代码正确无误,但FindOneAndUpdate
返回文档修改前({3}}。要更改此参数,您需要指定其他参数:
FindOneAndUpdateOptions<House> options = new FindOneAndUpdateOptions<House>();
options.ReturnDocument = ReturnDocument.After;
var result = collection.FindOneAndUpdate(filter, update, options);