I have the following model:
public class Car
{
public string Id {get; set;}
public IList<Driver> Drivers {get; set;}
}
public Driver
{
public string Id {get; set;}
public string IsActive {get; set;}
{
I want to set false for IsActive
property in all documents for specific car. So, I have the following code:
var carId = ...;
var setInactive = Builders<Car>.Update
.Set();
await _carCollection.FindOneAndUpdateAsync(a => a.Id == carId, setInactive);
What I should write in Set
method to set false
for all documents in the list?
UPDATE I read the comment by @Neil. So, I wrote the following code:
var carId = "...";
var drivers = await _carCollection.Find(a => a.Id == ObjectId.Parse(carId))
.Project(a=>a.Drivers)
.SingleAsync();
UpdateDefinition<Car> makeInactive = new UpdateDefinitionBuilder<Car>()
.Set(a => a.Drivers[0].IsActive, false);
for (var i = 1; i < drivers.Count; i++)
{
var index = i;
makeInactive = makeInactive.Set(a => a.Drivers[index].IsActive, false);
}
await _carCollection.FindOneAndUpdateAsync(a => a.Id == ObjectId.Parse(carId), makeInactive);
It works fine, but it's so ugly. Is there another way?