Home > OS >  MongoDB: Update array elements on multiple conditions
MongoDB: Update array elements on multiple conditions

Time:03-01

Colleagues, good afternoon! I'm struggling with some issue. I am using MongoDB 4.4.4. My assignment looks like this:

  1. There is a set of elements methods[].subcategories[].actions[]. Please note that all objects are arrays and they may be absent. Elements of the actions[] array consists of the _id and title fields.
  2. It is necessary to find the actual value of recordId by the field actions.title of the element and write it to the element of the actions array.

List of current values: 0af4cd2e-78cb-109b-8178-d5a7ba0e0012, Inspection 0af4cd2e-78cb-109b-8178-d5a7ba130014, Screening 0af4cd2e-78cb-109b-8178-d5a7ba170016, Poll 0af4cd2e-78cb-109b-8178-d5a7ba1b0018, Getting written explanations 0af4cd2e-78cb-109b-8178-d5a7ba1e001a, Request for documents 0af4cd2e-78cb-109b-8178-d5a7ba21001c, Sampling (samples) 0af4cd2e-78cb-109b-8178-d5a7ba23001e, Instrumental examination 0af4cd2e-78cb-109b-8178-d5a7ba260020, Test 0af4cd2e-78cb-109b-8178-d5a7ba2b0022, Expertise 0af4cd2e-78cb-109b-8178-d5a7ba2d0024, Experiment 3b7205c1-8282-4b63-8121-b82aacd7ca67, Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control

I wrote the following code:

db.getSiblingDB("ervk_core").getCollection("supervision1").updateMany(
    {},
    {
        "$set": {
            "methods.subcategories.actions.$[elem1]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0012",
            "methods.subcategories.actions.$[elem2]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0014",
            "methods.subcategories.actions.$[elem3]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0016",
            "methods.subcategories.actions.$[elem4]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0018",
            "methods.subcategories.actions.$[elem5]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001a",
            "methods.subcategories.actions.$[elem6]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001c",
            "methods.subcategories.actions.$[elem7]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001e",
            "methods.subcategories.actions.$[elem8]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260020",
            "methods.subcategories.actions.$[elem9]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260022",
            "methods.subcategories.actions.$[elem10]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260024",
            "methods.subcategories.actions.$[elem11]._id": "3b7205c1-8282-4b63-8121-b82aacd7ca67",
        }
    },
    {
        "arrayFilters": [
            {
                "elem1.title": "Inspection",
            },
            {
                "elem2.title": "Search",
            },
            {
                "elem3.title": "Poll",
            },
            {
                "elem4.title": "Receipt of Written Explanations",
            },
            {
                "elem5.title": "Retrieval of Documents",
            },
            {
                "elem6.title": "Sampling (samples)",
            },
            {
                "elem7.title": "Instrumental examination",
            },
            {
                "elem8.title": "Trial",
            },
            {
                "elem9.title": "Expertise",
            },
            {
                "elem10.title": "Experiment",
            },
            {
                "elem11.title": "Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control",
            }
        ]
    }
);

However, it gives the error "The path 'methods.subcategories.actions' must exist in the document in order to apply array updates.". I understand why it occurs - due to the absence of the actions[] array. But how can I account for the fact that methods[].subcategories[].actions[] arrays may be missing. And did I write the code correctly, otherwise I'm already a little confused. Thanks a lot in advance!

CodePudding user response:

What you can do is add array filters checks to the method and subcategory object to see the nested array exists, this will solve your issue as Mongo will not continue checking the nested conditions in case they don't exist. Here's how you'd do it:

db.collection.updateMany(
{
  "methods.subcategories.actions": {
    $exists: true
  }
},
{
  "$set": {
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem1]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0012",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem2]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0014",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem3]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0016",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem4]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0018",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem5]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001a",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem6]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001c",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem7]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001e",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem8]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260020",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem9]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260022",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem10]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260024",
    "methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem11]._id": "3b7205c1-8282-4b63-8121-b82aacd7ca67"
  }
},
{
  "arrayFilters": [
    {
      "methodElem.subcategories": {
        $exists: true
      }
    },
    {
      "subCatElem.actions": {
        $exists: true
      }
    },
    {
      "elem1.title": "Inspection"
    },
    {
      "elem2.title": "Search"
    },
    {
      "elem3.title": "Poll"
    },
    {
      "elem4.title": "Receipt of Written Explanations"
    },
    {
      "elem5.title": "Retrieval of Documents"
    },
    {
      "elem6.title": "Sampling (samples)"
    },
    {
      "elem7.title": "Instrumental examination"
    },
    {
      "elem8.title": "Trial"
    },
    {
      "elem9.title": "Expertise"
    },
    {
      "elem10.title": "Experiment"
    },
    {
      "elem11.title": "Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control"
    }
  ]
})

Mongo Playground

I also changed the updates query to just ignore documents with no nested actions, this is just to save time.

  • Related