MongoDB: Update array elements on multiple conditions

Colleagues, good afternoon! I’m struggling with some issues. 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 consist of the _id and title fields.
  2. It is necessary to find the actual value recorded 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 which I got from mongoDB interview questions:

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!

Hi @cloudy_tech,

Can you please share a document before and after the update? Like starting point / end point? I’m struggling to understand what you are trying to do exactly. If you could also clarify the input that you have (what you use to create your update query) - I think this would help.
Also is it an updateOne or actually an updateMany? If it’s a “many” then many provide an example with a couple of docs?

Cheers,
Maxime.

1 Like