0

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"
        }
      ]
    }
  ]
}
Til
  • 5,150
  • 13
  • 26
  • 34
SNC
  • 1
  • 2

3 Answers3

0

Please, can you check here once? Criteria.where("cities._id").is(cityId). Here I thin we have to use "cities.id".

Ravi
  • 424
  • 3
  • 13
0

To remove a specific city document:

Update changed = new Update().pull("cities", new Query(Criteria.where("id").is(cityId)));
mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(username)), changed, Person.class);

To add new city information:

Update update = new Update().addToSet("cities",city);
Himanshu Sharma
  • 2,940
  • 1
  • 7
  • 18
0

Thanks for the answers MrSSharma and Ravi.

Now I am deleting it and pushing updated document in the list,instead of updating subdocument entity. And It is working for me. after accessing object id by doing toHexString.

Delete operation update.pull("cities", Query.query(Criteria.where("id").is(id.toHexString()))); mongoOperations.upsert(query, update, Person.class);

Add operation: update.push("cities", subdocumentEntity ); mongoOperations.updateFirst(query, update, Person.class));

SNC
  • 1
  • 2