我与 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 并想要更新法国 - 巴黎子文档。
{"_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"
}
]
}
]
}