使用Node.js和MongoDB更新嵌入式文档的各个部分

时间:2018-08-07 15:26:27

标签: node.js mongodb

我收集了以下mongodb文档:taxTags

在这里,我想更新嵌入式文档的某些部分,在本例中为tagContents部分。

我想将第一个document.taxContents.displayname从TestTag1更新到TestTag11,将第二个document.taxContents.displayname从TestTag2更新到TestTag22。

[
  {
    "_id": "5b69a13a94403949a0f88d8f",
    "isPublished": false,
    "createdBy": "Admin",
    "updatedBy": "Admin",
    "parentTagId": 1,
    "tagContents": [
      {
        "_id": "5b69a13a94403949a0f88d8b",
        "displayName": "TestTag1",
        "languageId": 37
      },
      {
        "_id": "5b69a13a94403949a0f88d8c",
        "displayName": "TestTag2",
        "languageId": 50
      },
      {
        "_id": "5b69a38105a2953d6023d4ba",
        "displayName": "TestTag3",
        "languageId": 56
      }
    ],
    "tagRelatedCountries": [
      {
        "_id": "5b69a13a94403949a0f88d8d",
        "countryId": 10
      },
      {
        "_id": "5b69a13a94403949a0f88d8e",
        "countryId": 11
      }
    ],
    "createdDate": "2018-08-07T13:40:10.992Z",
    "updatedDate": "2018-08-07T13:40:10.992Z",
    "__v": 1
  },
  {
    "_id": "5b69a194390a7934901550e6",
    "isPublished": false,
    "createdBy": "Admin",
    "updatedBy": "Admin",
    "parentTagId": null,
    "tagContents": [
      {
        "_id": "5b69a194390a7934901550e2",
        "displayName": "TestTagE",
        "languageId": 37
      },
      {
        "_id": "5b69a194390a7934901550e3",
        "displayName": "TestTagF",
        "languageId": 50
      },
      {
        "_id": "5b69a3dd89b3ef0f8843b72d",
        "displayName": "TestTagG",
        "languageId": 56
      }
    ],
    "tagRelatedCountries": [
      {
        "_id": "5b69a194390a7934901550e4",
        "countryId": 100
      },
      {
        "_id": "5b69a194390a7934901550e5",
        "countryId": 111
      }
    ],
    "createdDate": "2018-08-07T13:41:40.664Z",
    "updatedDate": "2018-08-07T13:41:40.664Z",
    "__v": 1
  }
]

更新的文档:

[
      {
        "_id": "5b69a13a94403949a0f88d8f",
        "isPublished": false,
        "createdBy": "Admin",
        "updatedBy": "Admin",
        "parentTagId": 1,
        "tagContents": [
          {
            "_id": "5b69a13a94403949a0f88d8b",
            "displayName": "TestTag11",
            "languageId": 37
          },
          {
            "_id": "5b69a13a94403949a0f88d8c",
            "displayName": "TestTag22",
            "languageId": 50
          },
          {
            "_id": "5b69a38105a2953d6023d4ba",
            "displayName": "TestTag3",
            "languageId": 56
          }
        ],
        "tagRelatedCountries": [
          {
            "_id": "5b69a13a94403949a0f88d8d",
            "countryId": 10
          },
          {
            "_id": "5b69a13a94403949a0f88d8e",
            "countryId": 11
          }
        ],
        "createdDate": "2018-08-07T13:40:10.992Z",
        "updatedDate": "2018-08-07T13:40:10.992Z",
        "__v": 1
      },
      {
        "_id": "5b69a194390a7934901550e6",
        "isPublished": false,
        "createdBy": "Admin",
        "updatedBy": "Admin",
        "parentTagId": null,
        "tagContents": [
          {
            "_id": "5b69a194390a7934901550e2",
            "displayName": "TestTagE",
            "languageId": 37
          },
          {
            "_id": "5b69a194390a7934901550e3",
            "displayName": "TestTagF",
            "languageId": 50
          },
          {
            "_id": "5b69a3dd89b3ef0f8843b72d",
            "displayName": "TestTagG",
            "languageId": 56
          }
        ],
        "tagRelatedCountries": [
          {
            "_id": "5b69a194390a7934901550e4",
            "countryId": 100
          },
          {
            "_id": "5b69a194390a7934901550e5",
            "countryId": 111
          }
        ],
        "createdDate": "2018-08-07T13:41:40.664Z",
        "updatedDate": "2018-08-07T13:41:40.664Z",
        "__v": 1
      }
    ]

有人可以帮助我解决此问题吗?

0 个答案:

没有答案