2

The following code allows us to update all documents in customerDetail collection where customer_user_id is 1:

db.getCollection("customerDetail")
        .updateMany(Filters.eq("customer_user_id", 1),
                Updates.combine(
                        Updates.set("birth_year", "birth_year"),
                        Updates.set("country", "country")
                ));

but I need to update ALL documents in the collection, so I need to find a way how to ask Java Driver do not apply any filters to update query, but as I can see for updateMany method Filter is a mandatory attribute and I can't just pass null.

So how can I update all documents?

ernest_k
  • 44,416
  • 5
  • 53
  • 99
user471011
  • 7,104
  • 17
  • 69
  • 97

1 Answers1

4

One option which I use frequently

mongoCollectionObject
        .updateMany(new Document(), //
                new Document("$set"
                        new Document("birth_year", "birth_year")
                        .append("country", "country")
                ));

First one is the condition - as it is empty - equivalent to {} - means all documents

second one is the document to be set for all matching documents

Gibbs
  • 21,904
  • 13
  • 74
  • 138