0

I have a collection down below. I am trying to update an array element.

I am trying to update if lineItem _id value is 1 then go to spec list and update characteristicsValue from 900 to 50 if specName is "Model", as you can see, _id is also an array.

collection data:

    {
    "_id": "100",
    "name": "Campaign",
    "status": "Active",
    "parts": {
        "lineItem": [
            {
                "_id": [
                    {
                        "name": "A",
                        "value": "1"
                    }
                ],
                "spec": [
                    {
                        "specName": "Brand",
                        "characteristicsValue": [
                            {
                                "value": "500"
                            }
                        ]
                    },
                    {
                        "specName": "Model",
                        "characteristicsValue": [
                            {
                                "value": "900"
                            }
                        ]
                    }
                ]
            },
            {
                "_id": [
                    {
                        "name": "B",
                        "value": "2"
                    }
                ],
                "spec": [
                    {
                        "specName": "Brand",
                        "characteristicsValue": [
                            {
                                "value": "300"
                            }
                        ]
                    },
                    {
                        "specName": "Model",
                        "characteristicsValue": [
                            {
                                "value": "150"
                            }
                        ]
                    }
                ]
            },
            {
                "_id": [
                    {
                        "name": "C",
                        "value": "2"
                    }
                ]
            }
        ]
    }
}

related update doesnt work as I expected.

 db.Collection.update({"parts.lineItem._id.value" : "1", 
    "parts.lineItem.spec.specName" : "Model"  },{ $set: { 
    "parts.lineItem.spec.$.characteristicsValue" : "50" } })

EDIT: Every _id has a spec array. so, we need to find _id and then go to spec under _id array, find the brand and update the value.

1 Answer 1

1

Try this way:

db.Collection.update(
   {},
{ $set: { "parts.lineItem.$[outer].spec.$[inner].characteristicsValue" : "50" } },
{ multi: true, arrayFilters: [{"outer._id.value" : "1"}, {"inner.specName" : "Model"}]}
);
Sign up to request clarification or add additional context in comments.

2 Comments

error is : The path 'parts.lineItem.0.spec' must exist in the document in order to apply array updates. because, some lineItem elements dont have spec, just some of them have it
Ok. With this new information I updated the answer. Try again please. =)

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.