【发布时间】:2019-08-03 22:24:48
【问题描述】:
我与营销人员在 MongoDB 中有不同城市地址的文档。
{
"_id": "name",
"cities": [
{
"id": "1234abc",
"city_name": "London",
"country": "England",
"addresses": [
{
"Home": "Unit 14 Edgar Buildings George Street"
},
{
"Office": "Studio 103 The Business Centre 61"
}
]
},
{
"id": "1234xyz",
"city_name": "Paris",
"country": "France",
"addresses": [
{
"Home": "102 rue La Boétie"
},
{
"Office": " IMPASSE VIVALDI VAUCE"
}
]
}
]
}
我的实体类是 Person、city 和 address。
如果那个人转移到另一个国家,比如 Canda,并且想要更新 France - Paris 子文档。
{"_id": "name",
{
"id": "1234xyz",
"city_name": "Toronto",
"country": "Canada",
"addresses": [
{
"Home": "Toronto City Hall 100 Queen St W"
},
{
"Office": "4110 Yonge StNorth York, ON M2P 2H3"
}
]
}
}
我不知道哪个 Spring Data Mongo api 将在这里工作。
我已尝试使用以下代码,但没有任何更新。
Update changed = new Update().pull("cities", new Query(Criteria.where("_id").is(username)));
mongoTemplate.updateFirst(new Query(Criteria.where("cities._id").is(cityId)), changed, Person.class);
如果我使用Update update = new Update().set("cities",city);,所有子文档都会被一个城市替换
预期输出
{
"_id": "name",
"cities": [
{
"id": "1234abc",
"city_name": "London",
"country": "England",
"addresses": [
{
"Home": "Unit 14 Edgar Buildings George Street"
},
{
"Office": "Studio 103 The Business Centre 61"
}
]
},
{
"id": "1234xyz",
"city_name": "Toronto",
"country": "Canada",
"addresses": [
{
"Home": "Toronto City Hall 100 Queen St W"
},
{
"Office": "4110 Yonge StNorth York, ON M2P 2H3"
}
]
}
]
}
【问题讨论】:
标签: java spring mongodb nested-documents