2

I have a document in my collection like below. I want to update specific object value. Please solve this. How to update isChecked : "0"

    {
    "STUDENT_001": {
    "name": "name1",
    "hobbies": {
        "01": {
            "isChecked": "1",
            "name": "hobby - name"
        },
        "02": {
            "isChecked": "0",
            "name": "hobby-name"
        }
    }
    },
    "STUDENT_002": {
    "name": "name2",
    "hobbies": {
        "01": {
            "isChecked": "1",
            "name": "hobby - name"
        },
        "02": {
            "isChecked": "1",
            "name": "hobby-name"
        }
    }
    }
}

2 Answers 2

2

It should be like:

.update(
  query,
  new BasicDBObject(
    "$set",
    new BasicDBObject("STUDENT_001.hobbies.01.isChecked", "0")
));

here query to match the document. and $set is update the field in the path specified i.e "STUDENT_001.hobbies.01.isChecked"

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

2 Comments

Thanks for your answer Jayaram Pradhan, but problem is that STUDENT_001 is hard coded here, is there any way to use this dynamically.
with Mongo3: (query, set("where.nested", valu))
0
MongoTemplate mongoTemplate;

mongoTemplate.updateFirst(
        new Query(Criteria.where("STUDENT_001.hobbies.01.isChecked").is("0")),
        new Update()
            .set("Update field name", "Update value"),
        yourObject.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.