Reputation: 485
How do I set a whole array in a subproperty of a Mongo document? This is my current document model, a house with rooms and each with furnitures.
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;}
}
This is my update method, but it is not working:
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);
}
How do I replace the content of one array with another brand new array?
Upvotes: 0
Views: 211
Reputation: 49945
Your code is correct but FindOneAndUpdate
returns the document before modification (docs). To change that you need to specify additional parameter:
FindOneAndUpdateOptions<House> options = new FindOneAndUpdateOptions<House>();
options.ReturnDocument = ReturnDocument.After;
var result = collection.FindOneAndUpdate(filter, update, options);
Upvotes: 1