0

Spring boot mongo repository support saveAll,but it cause unique index insert error ,so I want use update.

In my case,I want to insert or update 10 documents together.

how to use updateAll?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
candrwow
  • 511
  • 5
  • 21
  • did you send `unique` index in any of your document fields ? – 0.sh Mar 29 '19 at 17:23
  • 1
    Spring Mongo has an `upsert()` method which can either "match and update" or "insert" a new document. But you probably mean "Bulk Operations" using an "upsert" for each item. Read more on ["upserts"](https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-option) in the documentation – Neil Lunn Mar 30 '19 at 00:09
  • Thank you,Bulk Operations is helpful,this link:https://stackoverflow.com/a/37030773/6431509 work. – candrwow Mar 30 '19 at 05:53

2 Answers2

0

Though I'm not crystal clear on your question, it sounds like you need to append { multi: true } to your update query. Please see Update Multiple Documents.

Setting the multi option updates all documents found by match.

To update multiple documents, set the multi option to true. For example, the following operation updates all documents where stock is less than or equal to 10:

db.books.update(
   { stock: { $lte: 10 } },
   { $set: { reorder: true } },
   { multi: true }
)
It'sNotMe
  • 1,184
  • 1
  • 9
  • 29
0

you can use updateMany:

db.collections.updateMany({ query }, {"$set":{ update }}
Ali Hallaji
  • 3,712
  • 2
  • 29
  • 36