我要求它需要更新具有相同属性的所有嵌入文档。
e.g. `Person oneToMany Addresses`
这里的地址是嵌入式文档,我想编写一个查询,如果对于嵌入式文档,我写city="New York"
我的所有嵌入文档都应该更新为city="New York"
。
编辑:
我已经提供了父文件' id'在嵌入文件中
以下是方法:
public Boolean updateAddressIsolataed(AddressModel addressModel) {
final Query query = new Query();
query.addCriteria(
Criteria.where("$isolated").is(1).and(PersonEntity.Address)
.elemMatch(Criteria.where("parentId").is(addressModel.getParentId())));
System.out.println("query:" + query.toString());
final Update update = new Update();
update.set(PersonEntity.Address + ".$." + "city", addressModel.getCity());
WriteResult wr = mongoTemplate.updateMulti(query, update, PersonEntity);
return wr.isUpdateOfExisting();
如果需要更多解释,请问我。 提前谢谢。
编辑2:
示例文档:
{
"_id" : ObjectId("571da15563500213286936bf"),
"_class" : "com.sample.entities.PersonEntity",
"name" : "Asin",
"age" : "22",
"address" : [
{
"_id" : ObjectId("5735acc5b29cadaf65a7f822"),
"parentId" : "571da15563500213286936bf",
"city" : "New York",
"state" : "QWERTY",
"zipcode" : 500
},
{
"_id" : ObjectId("5735acc5b29cadaf65a7f822"),
"parentId" : "571da15563500213286936bf",
"city" : "Mumbai",
"state" : "Maharashtra",
"zipcode" : 500
}
]
}