mongoose 如何在mongodb中更新数组的嵌套对象如下文档,解释如下:

sq1bmfud  于 2023-05-13  发布在  Go
关注(0)|答案(1)|浏览(112)

我有以下文件。我正在编写一个API来将"dataTracking"更新为truefalse。我正在尝试基于"userID""ulcaApiKey""serviceProviderName"进行更新。

{
  "_id": {
    "$oid": "64462ca4ff7e6dfc24d0eaeab"
  },
  "userID": "6a73afa4fc774dcdb28d5fffsd944511fD",
  "email": "samsaw.somseeyou@gmail.com",
  "firstName": "SAM SAW Master",
  "password": "$2b$12$IMYAkfdXBruXJm75O9P79.cZZHTA0SB9fvJDTN.cg7Fwtu/VBg/JS",
  "isVerified": true,
  "isActive": true,
  "roles": [
    "SAMUEL-DATASETS-CONTRIBUTOR"
  ],
  "apiKeyDetails": [
    {
      "appName": "apspDs",
      "ulcaApiKey": "05ff4569ff-9cc1-4959-a39e-94e90e01de6s",
      "createdTimestamp": "1681467785",
      "serviceProviderKeys": [
        {
          "serviceProviderName": "maddyyyyys",
          "dataTracking": "true",
          "inferenceApiKey": {
            "name": "Authorization",
            "value": "VBM0sd4D82pQsdckvUwBwI62pCP7sYuniYcA4-5cGS4LF77dUSfhxp-yxdvo4qglzJ8"
          }
        },
        {
          "serviceProviderName": "somwthing",
          "dataTracking": "false",
          "inferenceApiKey": {
            "name": "Authorization",
            "value": "qi5tpTvGXsdckMDDiqCfMk_UQIKab8QeRdt3Gse3ZnEnsCtitnMZilU0aPIHCZl6xaC"
          }
        },
        {
          "serviceProviderName": "somehtingfandall",
          "dataTracking": "false",
          "inferenceApiKey": {
            "name": "Authorsization",
            "value": "qi5tpTsdfvGXkMDDiqCfMk_UQIKab8QeRdt3Gse3ZnEnsCtitnMZilU0aPIHCZl6xaC"
          }
        }
      ]
    },
    {
      "appName": "app",
      "ulcaApiKey": "05ff4569ff-9cc1-4959-a39e-94e90e01de66",
      "createdTimestamp": "1681467785",
      "serviceProviderKeys": [
        {
          "serviceProviderName": "maddyyyyy",
          "dataTracking": "false",
          "inferenceApiKey": {
            "name": "Authorization",
            "value": "VBM0sd4D82pQkvUwBwI62pCP7sYuniYcA4-5cGS4LF77dUSfhxp-yxdvo4qglzJ8"
          }
        },
        {
          "serviceProviderName": "AI4Bharat",
          "dataTracking": "false",
          "inferenceApiKey": {
            "name": "Authorization",
            "value": "qi5tpTvGXkMDDiqCfMk_UQIKab8QeRdt3Gse3ZnEnsCtitnMZilU0aPIHCZl6xaC"
          }
        }
      ]
    },
    {
      "appName": "appDummy",
      "ulcaApiKey": "05ff4569ff-9cc1-4959-a39e-94e90e01de6D",
      "createdTimestamp": "1681467785",
      "serviceProviderKeys": [
        {
          "serviceProviderName": "dummyMaddy",
          "dataTracking": "true",
          "inferenceApiKey": {
            "name": "Authorization",
            "value": "VBM0sd4D82pQkvUwBwI62pCP7sYuniYcA4-5cGS4LF77dUSfhxp-yxdvo4qglzJ8"
          }
        }
      ]
    }
  ]
}

我尝试了下面的代码,但找不到结果。

db.getCollection("ulca-user").updateOne(
  {
    "userID": "6a73afa4fc774dcdb28d5fffsd944511fD",
    "apiKeyDetails.ulcaApiKey": "05ff4569ff-9cc1-4959-a39e94e90e01de6s",
    "apiKeyDetails.serviceProviderKeys.serviceProviderName": "maddyyyyys"
  },
  {
    $set:{
      "apiKeyDetails.serviceProviderKeys.dataTracking": "false"
    }
  }
)

如何使用"userID""ulcaApiKey""serviceProviderName"更新"dataTracking"

3zwtqj6y

3zwtqj6y1#

就像@ray评论的那样,"arrayFilters"非常适合这个用例。你可以这么做。

db.collection.update({
  "userID": "6a73afa4fc774dcdb28d5fffsd944511fD",
  "apiKeyDetails.ulcaApiKey": "05ff4569ff-9cc1-4959-a39e-94e90e01de6s",
  "apiKeyDetails.serviceProviderKeys.serviceProviderName": "maddyyyyys"
},
{
  "$set": {
    "apiKeyDetails.$[key].serviceProviderKeys.$[name].dataTracking": "false"
  }
},
{
  "arrayFilters": [
    {"key.ulcaApiKey": "05ff4569ff-9cc1-4959-a39e-94e90e01de6s"},
    {"name.serviceProviderName": "maddyyyyys"}
  ]
})

mongoplayground.net上试试。

相关问题