Reputation: 33
I want to insert new child on the record below and i use this code on .net core
var builder = Builders<parent>.Filter;
var filter = builder.Eq("_id", "123");
var update = Builders<parent>.Update.AddToSet<IEnumerable<Child>>
("Child", Child);
await _expensesContext.collection.UpdateOneAsync(filter, update);
parent
{
"_Id": "123"
"Child": [
{"Id": "1234", "name": "jhon"}
]
}
but i enconter this error Unable to cast object of type 'System.Collections.Generic.List`1[child]' to type 'Child'.'
Upvotes: 0
Views: 478
Reputation: 33
I fixed this using addtosetEach
instead of addtoset
:
var builder = Builders<parent>.Filter;
var filter = builder.Eq("_id", "123");
var update = Builders<parent>.Update.AddToSetEach<IEnumerable<Child>>
("Child", Child);
await _Context.collection.UpdateOneAsync(filter, update);
Upvotes: 1