I have a document in mongodb that has been created from this java model:
class Comment
{
String pollID;
List<CommentDetail> commentDetailList;
}
class CommentDetailList
{
String text;
User user;
}
class User
{
String userID;
String username;
}
So, my document will look like this:
{
"pollID":"ABCDEFG",
"commentDetailList":
[
{
"text":"Hello Comment1",
"user":
{
"userID":"001",
"username": "username1"
}
},
{
"text":"Hello Comment2",
"user":
{
"userID":"001",
"username": "username1"
}
},
{
"text":"Hello Comment3",
"user":
{
"userID":"002",
"username": "username2"
}
}
]
}
Now I want to update username of user whose userID = 001 with this code:
Query query = new Query(Criteria.where("pollID").is("ABCDEFG")
.and("commentDetailList")
.elemMatch(Criteria.where("user.userID").is("001")));
Update update = new Update().set("commentDetailList.$.user.username", username);
WriteResult wr = mongoTemplate.updateMulti(query, update, "comment");
The problem is it update only first comment (comment with text = "Hello Comment1").
Could anyone help me, please ?
Do I have misunderstand with the update function ?
Thx.
PS. Sorry for my english :D
Try this query:
Query query = new Query(new Criteria().andOperator(
Criteria.where("pollID").is("ABCDEFG"),
Criteria.where("commentDetailList").elemMatch(Criteria.where("user.userID").is("001"))
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With