0

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?

user348173
  • 8,818
  • 18
  • 66
  • 102
  • For "all". You cannot. See [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – Neil Lunn Jul 03 '17 at 12:11
  • @NeilLunn Thanks for the comment, I have updated question, could you have a look, if I did it right way. – user348173 Jul 03 '17 at 12:40

0 Answers0