I am quite new to Spring Boot and MongoDB. I am trying to update the availability for a specific user within the user array. The issue is that I am trying to find a specific username for example: "Mark" and update that entry only. However, it could be under any index of the array, and I'm not quite sure how write that in the query statement.
The structure of my data in mongoDB:
{
"name": "test meeting",
"url": "temporary_url",
"timezone": "pacific",
"startTime": 8,
"endTime": 21,
"users": [
{
"username": "Mark",
"password": "Caddy",
"availability": { << TRYING TO UPDATE THIS
//array of objects
}
},
{
"username": "Bill",
"password": "Bobby",
"availability": {
//array of objects
}
}
],
"_class": "com.dcproduction.meetapp.classes.Meeting"
}
Below is my attempt to make it work. It works if I substitute an index for [WHAT GOES HERE] such as '0', but the point of this query is that I wouldn't normally know which index the name would fall under:
Query query = new Query();
query.addCriteria(
new Criteria().andOperator(
Criteria.where("name").is("test meeting"),
Criteria.where("users.[WHAT GOES HERE?]username").is("Mark")));
Update testUpdate = new Update();
testUpdate.set("users[WHAT GOES HERE].availability", some_data);
UpdateResult updateResult = mongoTemplate.updateFirst(query, testUpdate, Meeting.class);
Any help would be appreciated, thanks!
You could use identifiers as mentioned here: https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/ which would be of the form:
query.addCriteria(
Criteria.where("users._username").is("Mark")
);
update.set("users.$[selector].availability", some_data)
.filterArray(Criteria.where("selector._username").is("Mark"));