我有这样的文件。
{
"id": "5a212b985735dd44089e4782",
"people": [{
"personId": "5a212b985735dd44089e4783",
"name": "Ronaldo",
"parents": [{
"parentId": "5a212b985735dd44089e4784",
"name": "Messi",
"address": [{
"addressId": "5a212b985735dd44089e4785",
"country": "Argentina",
"city": "Blah Blah"
},
{
"addressId": "5a212b985735dd44089e4786",
"country": "USA",
"city": "New York"
}
]
}]
}]
}
我必须使用新的父对象替换父数组中的现有父对象。
我写的代码是:
Query query = new Query(Criteria.where("id").is("5a212b985735dd44089e4782"));
Update update = new Update().push("people.$.parents", parent);
this.mongoTemplate.findAndModify(query, update, PeopleInfo.class);
但是,它不是替换现有的父对象,而是创建一个新对象。
有谁知道如何执行此类查询?
答案 0 :(得分:0)
请阅读push
here的文档。在这种情况下,您应该做的是set
而不是push
Query query = new Query(Criteria.where("id").is("5a212b985735dd44089e4782"));
Update update = new Update().set("people.parents", parent);
this.mongoTemplate.findAndModify(query, update, PeopleInfo.class);