【问题标题】:update all the elements of subdocument where subdoucment is part of list更新子文档的所有元素,其中子文档是列表的一部分
【发布时间】: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


    【解决方案1】:

    请问,您可以在这里检查一次吗? Criteria.where("cities._id").is(cityId)。 在这里我认为我们必须使用"cities.id"

    【讨论】:

      【解决方案2】:

      要删除特定的城市文档:

      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);
      

      添加新的城市信息:

      Update update = new Update().addToSet("cities",city);
      

      【讨论】:

        【解决方案3】:

        感谢 MrSSharma 和 Ravi 的回答。

        现在我正在删除它并在列表中推送更新的文档,而不是更新子文档实体。它对我有用。通过 toHexString 访问对象 id 后。

        删除操作 update.pull("城市", Query.query(Criteria.where("id").is(id.toHexString()))); mongoOperations.upsert(查询、更新、Person.class);

        添加操作: update.push("城市", subdocumentEntity ); mongoOperations.updateFirst(query, update, Person.class));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-31
          • 2017-08-25
          • 2015-12-22
          • 1970-01-01
          • 2017-06-13
          • 2018-07-29
          • 2019-06-21
          • 2011-07-31
          相关资源
          最近更新 更多