Here is the MongoDB document that I want to update the serviceName first then add or update both for nested object of vehicle at the same time. Anyone can help to achieve this in C#?
{
"serviceName": "Service1",
"intervalKm": 3219,
"dueSoonKm": 161,
"vehicle": [
{
"vehicleId": "9100ffff-48a9-ca63-e418-22ca712b0000",
"dueAtKm": 3228,
"untilDueKm": 3219
},
{
"vehicleId": "9100ffff-48a9-ca63-e430-22ca712b0000",
"dueAtKm": 3219,
"untilDueKm": 3219
}
]
}
Add and Update both in nested object of MongoDB
Below is my code and it's working for main object but not for nested object.
Var result = _mongoCollection.UpdateOneAsync( Builders<ServiceModel>.Filter.Eq(p => p.ServiceId, serviceId), Builders<ServiceModel>.Update .Set(rec => rec.ServiceName, request.ServiceName) .Set(rec => rec.IntervalKm, request.IntervalKm) .Set(rec => rec.DueSoonKm, request.DueSoonKm) .ConfigureAwait(false);
Now I want to update and add object in nested array of vehicle? so, my question how we can add new item in nested array and update if already available? I hope my question is clear now.