7

In my project I'm using SpringBoot 1.3.2 and org.springframework.data.mongodb.core.query.*

I'm trying to update element in array, in my main object i have array looking like this:

"sections" : [
        {
                "sectionId" : "56cc3c908f5e6c56e677bd2e",
                "name" : "Wellcome"
        },
        {
                "sectionId" : "56cc3cd28f5e6c56e677bd2f",
                "name" : "Hello my friends"
        }
]

Using Spring I want to update name of record with sectionId 56cc3c908f5e6c56e677bd2e

I was trying to to this like that but it didn't work

  Query query = Query.query(Criteria
                .where("sections")
                .elemMatch(
                        Criteria.where("sectionId").is(editedSection.getId())
                )
        );
        Update update = new Update().set("sections", new BasicDBObject("sectionId", "56cc3c908f5e6c56e677bd2e").append("name","Hi there"));
        mongoTemplate.updateMulti(query, update, Offer.class);

It create something like:

"sections" : {
         "sectionId" : "56cc3c908f5e6c56e677bd2e",
         "name" : "Hi there"
 }

But this above is object { } I want an array [ ], and I don't want it remove other elements.

Can any body help me how to update name of record with sectionId 56cc3c908f5e6c56e677bd2e using Spring

3 Answers 3

20

You essentially want to replicate this mongo shell update operation:

db.collection.update(
    { "sections.sectionId": "56cc3c908f5e6c56e677bd2e" },
    {
        "$set": { "sections.$.name": "Hi there" }
    },
    { "multi": true }
)

The equivalent Spring Data MongoDB code follows:

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.mongodb.core.query.Update;

...

WriteResult wr = mongoTemplate.updateMulti(
    new Query(where("sections.sectionId").is("56cc3c908f5e6c56e677bd2e")),
    new Update().set("sections.$.name", "Hi there"),
    Collection.class
);
Sign up to request clarification or add additional context in comments.

2 Comments

How can I update if the field to be updated is inside a nested array? Below is my object { "id": "5e97e89963a66e2609d5ae91", "Name": "Emhye", "lastName":"Emi", "calendarEvents": [ { "id": "57", "description": "" }, { "id": "42", "description": "" }, ] } I need to edit 2nd element of the CalendarEvents array
@chridam this way we need to explicitly mention each and every field to update, is there anyway where I could just send pojo object and it will set if exists modify field or else add them to object.
2

Can use BulkOperations approach to update list or array of document objects

BulkOperations bulkOps = mongoTemplate.bulkOps(BulkMode.UNORDERED, Person.class);
for(Person person : personList) {
    Query query = new Query().addCriteria(new Criteria("id").is(person.getId()));
    Update update = new Update().set("address", person.setAddress("new Address"));
    bulkOps.updateOne(query, update);
}
BulkWriteResult results = bulkOps.execute();

Comments

0

Thats my solution for this problem:

  public Mono<ProjectChild> UpdateCritTemplChild(
       String id, String idch, String ownername) {

    Query query = new Query();
    query.addCriteria(Criteria.where("_id")
                              .is(id)); // find the parent
    query.addCriteria(Criteria.where("tasks._id")
                              .is(idch)); // find the child which will be changed

    Update update = new Update();
    update.set("tasks.$.ownername", ownername); // change the field inside the child that must be updated

    return template
         // findAndModify:
         // Find/modify/get the "new object" from a single operation.
         .findAndModify(
              query, update,
              new FindAndModifyOptions().returnNew(true), ProjectChild.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.