2

I'm trying to execute the following query:

db.getCollection("item").update({},
    {
        "$set":{"questions.optional.$[].items.$[item_field].name":"immediately"}
    },
    {
        "arrayFilters":
        [
            {"item_field.id": "5bf3c907449f4978200dd581"}
        ],
        multi: true
    }
)

But the result that I get is this error:

"Error parsing array filter :: caused by :: The top-level field name must be an alphanumeric string beginning with a lowercase letter, found 'items_field'"

This is the simplified version of the document:

{
  "_id": ObjectId("12345678"),
  "questions": {
    "mandatory": [
      {
        "name": "question name",
        "mandatory": true,
        "items": [
          {
            "id": 1,
            "name": "option1 value"
          }
        ]
      }
    ],
    "optional": [
      {
        "name": "question name",
        "mandatory": false,
        "items": [
          {
            "id": 4,
            "name": "option4 value"
          }
        ]
      }
    ]
  }
}

What does this error mean? How can I avoid it?

1 Answer 1

4

In your case the problem is in the name "item_field". MongoDB does not support the underscore character in this scenario. Changing the name "item_field" to "itemField" resolves the update command:

Update:

db.getCollection("item").update({},
    {
        "$set":{"questions.optional.$[].items.$[itemField].name":"immediately"}
    },
    {
        "arrayFilters":
        [
            {"itemField.id": 4}
        ],
        multi: true
    }
)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much. I couldn't find the relevant documentation anywhere. you saved life

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.