简体   繁体   中英

Update document with its value

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM