0

I have a Mongo db in Spring boot application (spring-data-mongodb is 2.1.5).

I want to update all documents by replacing their json. Get each one, modify it with jaskson and put back. Is it possible with using MongoTemplate or some other API in spring-data-mongodb?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
user3636486
  • 409
  • 1
  • 5
  • 11
  • I found the answer here https://stackoverflow.com/questions/41768431/what-is-the-correct-method-to-update-a-document-while-using-mongo-cursor – user3636486 May 12 '20 at 11:17
  • 2
    In MongoDB there are two collection methods [findOneAndReplace](https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/) and [replaceOne](https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/) to replace a document. Spring's [MongoTemplate](https://docs.spring.io/spring-data/mongodb/docs/2.1.17.RELEASE/api/) has the `findAndReplace` method. – prasad_ May 12 '20 at 11:19
  • 1
    Check this answer https://stackoverflow.com/a/49815728/5330655 , as he said just use save() on MongoTemplate – Rebai Ahmed May 12 '20 at 12:53

0 Answers0