0

I have this following document. I'm writing an API to update "dataTracking" to either true or false. I'm trying to update based on "userID", "ulcaApiKey", and "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"
          }
        }
      ]
    }
  ]
}

I tried the following code but I couldn't find the result.

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

How do I update "dataTracking" using "userID", "ulcaApiKey", and "serviceProviderName"?

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • 2
    Does this answer your question? [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – ray May 10 '23 at 15:45
  • @ray this does not solve my question as my document is nested differently than the document in comment/link. – notifier test May 10 '23 at 15:53
  • like how? Have you checked out the demonstration of `arrayFilters` in the link? It is the canonical solution to update array elements, regardless of level of array nesting. – ray May 10 '23 at 15:58

1 Answers1

0

Like @ray commented, "arrayFilters" are very well suited for this use case. Here's one way you could do it.

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"}
  ]
})

Try it on mongoplayground.net.

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42