Announcement Announcement Module
Collapse
No announcement yet.
MongoTemplate update behaviour question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoTemplate update behaviour question

    I have noticed that for some of the update operations (e.g. set), updateFirst/updateMulti needs to be called individually, while others (pop, push, inc) can be chained. E.g.


    Code:
    Query query = new Query(where("_id").is(person.getId()));
    
    List<String> emails = new ArrayList<String>();
    emails.add("[email protected]");
    emails.add("[email protected]");
    emails.add("[email protected]");
    
    Update update = new Update();
    
    update.set("firstName", person.getFirstName());
    mongoTemplate.updateFirst(query, update);
    update.set("lastName", person.getLastName());
    mongoTemplate.updateFirst(query, update);
    update.set("money", person.getMoney());
    mongoTemplate.updateFirst(query, update);
    update.set("emails",emails);
    mongoTemplate.updateFirst(query, update);
    update.set("emailCount",3);
    mongoTemplate.updateFirst(query, update);
    
    
    query = new Query(where("_id").is(person.getId()).and("emailCount").gt(3));
    update = new Update();
    update.pop("emails", Update.Position.LAST).inc("emailCount", -1);
    mongoTemplate.updateFirst(query, update);
    
    String newEmail = "[email protected]";
    query = new Query(where("_id").is(person.getId()));
    update = new Update();
    update.push("emails", newEmail).inc("emailCount", 1);
    mongoTemplate.updateFirst(query, update);
    If I try to chain set operations, only the last one succeeds.

    Is this expected behavior? If yes, is there a document explaining this?

    Thanks
Working...
X