1

I am trying to update a field inside array of objects, where field in nested array is equal to a value.

My goal here is to set the picture field a new url, where value field in valueList is oldRed

Product schema:

{
    variations: [{
        id: 1,
        picture: 'https://example.picture.com',
        valueList: [{
            name: 'color',
            value: 'oldRed'
        }, {
            name: 'size',
            value: 'M'
        }]

    }, {
        id: 2,
        picture: 'https://example.picture.com',
        valueList: [{
            name: 'color',
            value: 'black'
        }, {
            name: 'size',
            value: 'M'
        }]

    }]
}

The closest I get is thanks to this answer, where I update all my nested array fields that contains :'oldRed' . But my final goal is to update other field picture, based on nested array field.

db.Product.findOneAndUpdate({
    _id: '123'
}, {
    $set: {
        'variations.$[].valueList.$[nameField].value': 'newRed'
    }
}, {
    arrayFilters: [{
        'nameField.value': 'oldRed'
    }],
    new: true
}
});

1 Answer 1

1

Please try this :

db.Product.findOneAndUpdate(
        { _id: 123 },
        {
            $set: {
                'variations.$[item].valueList.$[nameField].value': 'newRed',
                'variations.$[item].picture': 'newURL' // item is each object in variations which is being checked in arrayFilters.
            }
        },
        {
            arrayFilters: [{ 'item.valueList.value': 'oldRed' }, { 'nameField.value': 'oldRed' }],
            new: true
        }
   )

Colletion Data :

{
    "_id" : 123,
    "variations" : [ 
        {
            "id" : 1,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }, 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }
            ]
        }, 
        {
            "id" : 2,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "black"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }, 
        {
            "id" : 3,
            "picture" : "https://example3.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }
    ]
}

Result :

/* 1 */
{
    "_id" : 123,
    "variations" : [ 
        {
            "id" : 1,
            "picture" : "newURL",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "newRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }, 
                {
                    "name" : "color",
                    "value" : "newRed"
                }
            ]
        }, 
        {
            "id" : 2,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "black"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }, 
        {
            "id" : 3,
            "picture" : "newURL",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "newRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think in mongoose it will be new: true to return updated document, if using mongodb driver or shell try returnNewDocument: true..

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.