1

My Document looks like this in MongoDB:

  {
    "_id": {
        "$oid": "6099d057b769cc513025fd54"
    },
    "key_name": "I_CAN_MATCH",
    "object_1": {
        "sub_object_array_1": [{
            "a": "TZNNI",
            "b": "R"
        }],
        "sub_object_array_2": [{
            "c": "SDLYA",
            "d": "N"
        }, {
            "c": "KTSMQ",
            "d": "N"
        }],
        "sub_object_array_3": [{
            "e": "KYCT0",
            "f": "F"
        }, {
            "e": "KYCT1",
            "f": "N"
        }, {
            "e": "KYCT2",
            "f": "F"
        }, {
            "e": "KYCT3",
            "f": "E"
        }],
    },
    "object_2": {
      "sub_object_4": { "and": "so on ..." }
    },
    "object_array_1": [{
            "g": "KYCT0",
            "h": "F"
        }, {
            "g": "KYCT1",
            "h": "N"
        }, {
            "g": "KYCT2",
            "h": "F"
        }, {
            "g": "KYCT3",
            "h": "E"
        }]
  }

My question is how to Update Array sub_object_array_3 where f = "F" to f = "U"?

The following does not work for me:

  await db_model.updateMany( 
    { key_name: "I_CAN_MATCH" },
    { $set: { "object_1.sub_object_array_3.$[array_element].f": "U" } },
    { arrayFilters: [ { "array_element.f": "F" } ] } );

But the following does! So I understand how the syntax works in this scenario.

  await db_model.updateMany( 
    { key_name: "I_CAN_MATCH" },
    { $set: { "object_array_1.$[array_element].h": "U" } },
    { arrayFilters: [ { "array_element.h": "F" } ] } );

I just don't know what I'm missing in the $set command when the Array is inside an Object first.

I have searched, but every search word I use points me to arrays in arrays, which is not the case here. Is anyone able to help me?

Thanks in advance!

2
  • Please remove fields which aren't relevant. Commented May 18, 2021 at 23:49
  • I'm not sure I understand what you mean. Commented May 18, 2021 at 23:51

1 Answer 1

3

Error - https://mongoplayground.net/p/Cvfc6oNyhrH

fail to run update: multiple write errors: [{write errors: [{Error parsing array filter :: caused by :: The top-level field name must be an alphanumeric string beginning with a lowercase letter, found 'array_element'}]}, {<nil>}]

Solution :-

Change array_element to arrayElement or anything without _

Note:- it should be alphanumeric

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

The must begin with a lowercase letter and contain only alphanumeric characters.


Demo - https://mongoplayground.net/p/GJku1bFaj3m

db.collection.update({
  key_name: "I_CAN_MATCH"
},
{
  $set: {
    "object_1.sub_object_array_3.$[arrayElement].f": "U"
  }
},
{
  arrayFilters: [
    { "arrayElement.f": "F" }
  ]
})
Sign up to request clarification or add additional context in comments.

1 Comment

Well, if you notice in my Document above, SUB_OBJECT_ARRAY_3 or any of the others, did not have an element with "_ID". That was the kicker! The arrayFilters statement seems to work on CoreMongooseArrays only. As soon as I incorporated the "_ID" (by Typing it) the above you have, or the one I had, worked! So we're all good. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.