1

Need to update a document based its value

var filter = Builders<UnitTravelHistory>.Filter.Empty;
var update = Builders<UnitTravelHistory>.Update.Set(i => i.JobDuration, i.A-i.B)
DBContext.ClientDb.Repository<UnitTravelHistory>(collection).UpdateMany(filter, update);

here A and B are 2 fields in the collection. Can anyone suggest a solution?

D.Rosado
  • 5,634
  • 3
  • 36
  • 56

1 Answers1

0

you need to use pipeline updates in order to refer to fields of the document being updated like so:

var pipelineStage = BsonDocument.Parse("{$set:{JobDuration:{$subtract:['$A','$B']}}}");

collection.UpdateMany(
    _ => true, 
    Builders<UnitTravelHistory>.Update.Pipeline(new[] { pipelineStage })
);

here's an alternative

Dĵ ΝιΓΞΗΛψΚ
  • 5,068
  • 3
  • 13
  • 26