0

I have a MongoDB database and the program I'm writing is meant to change the values of a single field for all documents in a collection. Now if I want them all to change to a single value, like the string value "mask", then I know that updateMany does the trick and it's quite efficient.

However, what I want is an efficient solution for updating to different new values, in fact I want to pick the new value for the field in question for each document from a list, e.g. an ArrayList. But then something like this

collection.updateMany(new BasicDBObject(), 
new BasicDBObject("$set",new BasicDBObject(fieldName, 
       listOfMasks.get(random.nextInt(size)))));

wouldn't work since updateMany doesn't recompute the value that the field should be set to, it just computes what the argument

listOfMasks.get(random.nextInt(size))

would be once and then it uses that for all the documents. So I don't think there's a solution to this problem that can actually employ updateMany since it's simply not versatile enough.

But I was wondering if anyone has any ideas for at least making it faster than simply iterating through all the documents and each time do updateOne where it updates to a new value from the ArrayList (in a random order but that's just a detail), like below?

// Loop until the MongoCursor is empty (until the search is complete)
        try {
            while (cursor.hasNext()) {

                // Pick a random mask
                String mask = listOfMasks.get(random.nextInt(size));

                // Update this document
                collection.updateOne(cursor.next(), Updates.set("test_field", mask));

            }
        } finally {
            cursor.close();
        }```
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
0314R
  • 1
  • 1

1 Answers1

0

MongoDB provides the bulk write API to batch updates. This would be appropriate for your example of setting the value of a field to a random value (determined on the client) for each document.

Alternatively if there is a pattern to the changes needed you could potentially use find and modify operation with the available update operators.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • Thanks, but what I'm really concerned about is time efficiency, and BulkWrite wouldn't help with that unless I'm missing something here. – 0314R Apr 14 '20 at 12:33
  • Bulk write API batches changes to a single request, so it would reduce network round-trips which is a time cost. – D. SM Apr 14 '20 at 19:29
  • Thanks! We'll look into it more – 0314R Apr 14 '20 at 20:48