I have document with Marketting person with different city addresses in 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"
}
]
}
]
}
My entity classes are Person, city, and address.
If that person shift to another country like Canda and wants update France - Paris subdocument.
{"_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"
}
]
}
}
I am clueless which Spring Data Mongo api will work here.
I have tried with below code, but nothing getting updated.
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);
And if I use Update update = new Update().set("cities",city);
all the subdocument gets replace with one city
Expected output
{
"_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"
}
]
}
]
}