9

Is it possible with Spring-Data-MongoDB to update multiple fields of a document with one query.

For example, I can run this mongo query:

db.customers.update(
  {"firstname": "Max"},
  { 
    $set: {
      "lastname": "Maier",
      "email": "[email protected]"
    }
  }
);

How can we achieve this with code and the spring MongoTemplate? For example here is the code to udpate one value:

Query select = Query.query(Criteria.where("firstname").is("Max"));
        Update updateValue = Update.update("lastname", "Maier");
        UpdateResult updateResult = mongoTemplate.updateFirst(select, updateValue, Customer.class);

It seems that the Update#set method accepts only one (key, value) and no multi-values or list of values.

4 Answers 4

15

It seems that the Update#set method accepts only one (key, value) and no multi-values or list of values.

Yes, it accepts only one key and value at a time. But you can do that for many keys and values as you want. It is like a Map, you can add as many keys(unique) and values as you like.

You just need to extend your code a bit

Query select = Query.query(Criteria.where("firstname").is("Max"));
Update update = new Update();
update.set("lastname", "Maier");
update.set("email", "[email protected]");
UpdateResult updateResult = mongoTemplate.findAndModify(select, update, Customer.class);

If you dig deep into findAndModify method, you can understand, behind the scenes it is holding a key value map.

Sign up to request clarification or add additional context in comments.

Comments

6

In my version findAndModifiy does not return UpdateResult. I had to use method updateMulti

Query select = Query.query(Criteria.where("firstname").is("Max"));
Update update = new Update();
update.set("lastname", "Maier");
update.set("email", "[email protected]");
UpdateResult updateResult = mongoTemplate.updateMulti(select, update, Customer.class);

Comments

3

You need findAndModify. Details and explanations there: https://www.mkyong.com/mongodb/spring-data-mongodb-update-document/

https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify

Comments

0

Below is the equivalent implementation using MongoTemplate for update operation.

For updating multiple fields one doc object

   public Person update(Person person){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(person.getId()));
        Update update = new Update();
        update.set("name", person.getName());
        update.set("description", person.getDescription());
        return mongoTemplate.findAndModify(query, update, Person.class);
    }

For updating same changes to multiple doc object based on query match

        Query query = new Query();
        query.addCriteria(Criteria.where("city").is(person.getCity()));
        Update update = new Update();
        update.set("pin", person.getPin());
        update.set("description", person.getDescription());
        mongoTemplate.updateMulti(query, update, Person.class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.