简体   繁体   中英

c# Mongodb 2.0 driver get UpdateManyAsync result

Is there any way to get the updated collections after/before the update? What I can think of is pretty heavy.

  1. get what i want to update using filter.
  2. update everything i got in the previous state
  3. get all by ids again and check what has been updated.

Why I need this?

I have a service which constantly fetch data from the database. I want it to take stuff atomically depending on the status they have.

For example, if a document, which contains the field Status, has a status of 1 i want the service to fetch it and atomically change it to 2 in the process, so every other component wont override me for some reason.

Update looks like:

var filter = Builders<ServiceModel>.Filter.And(
                                                                Builders<ServiceModel>.Filter.Gt(t => t.EndDate, dueDate),
                                                                Builders<ServiceModel>.Filter.Eq(t => t.Status, Enums.ServiceHandleStatus.Registered));

            var update = Builders<ServiceModel>.Update.Set(t => t.Status, Enums.ServiceHandleStatus.InProcess);

            var options = new UpdateOptions() { IsUpsert = false };

            using (var result = this.manager.Trainings.UpdateManyAsync(filter, update, options))

Any direction?

TIA.

The only atomic updates in mongodb like this is to use findAndModify, which is presented in .NET as FindOneAndUpdate. So, you can do what you want, but only a single document at a time.

Update

If you are ok making multiple requests, you can do this in 2 requests using UpdateMany and, instead of just marking them as InProcess, also indicate which process is processing them. Then, you can make a subsequent query to come back and pull all of those that are InProcess for the current process.

The only problem with that is you can't indicate to only update 10, or 20, or 10K, which means you can't scale out to multiple "processors" because you'll be updating all of them.

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