Updating nested array of objects

I have this in a collection:

"_id": "62be0271d373b2f2fc1826a2",
        "condition": "new",
        "variants": [
            {
                "color_name": "Green",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a4"
                    },
                    {
                        "size": "M",
                        "price": 100,
                        "quantity": 2,
                        "_id": "62be0271d373b2f2fc1826a5"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a3"
            },
            {
                "color_name": "Blue",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a7"
                    },
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a8"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a6"
            }
        ],
        "featured": true

I have this document in my mongodb collection:

        "_id": "62be0271d373b2f2fc1826a2",
        "condition": "new",
        "variants": [
            {
                "color_name": "Green",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a4"
                    },
                    {
                        "size": "M",
                        "price": 100,
                        "quantity": 2,
                        "_id": "62be0271d373b2f2fc1826a5"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a3"
            },
            {
                "color_name": "Blue",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a7"
                    },
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a8"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a6"
            }
        ],
        "featured": true

I want to update only the quantity of the items field with _id = “62be0271d373b2f2fc1826a8”

NOTE: there may be more variants

Helllo @Philip_Enaohwo,

Welcome to the community! :wave:

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, then you can try below.

db.collection.updateOne(
    {"variants.items._id" :  "62be0271d373b2f2fc1826a8"},
    {$set: {
        'variants.$[].items.$[xxx].quantity': 999
    }},
    {arrayFilters: [
        {"xxx._id": '62be0271d373b2f2fc1826a8'}
    ]}
)

Your particular use case includes Array within an Array, below is the explanation of the query.

Let me know if you have any more questions. Happy to help! :slight_smile:

Regards,
Tarun

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.