MongoDB-根据嵌套数组的字段值更新数组对象中的字段 [英] MongoDB - Update field in an array object based on nested array's field value

查看:200
本文介绍了MongoDB-根据嵌套数组的字段值更新数组对象中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新对象数组内部的字段,其中嵌套数组中的字段等于一个值.

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

我的目标是将 picture 字段设置为新的url,其中 valueList 中的 value 字段为 oldRed

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

产品架构:

{
    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'
        }]

    }]
}

我得到的最接近的感谢是这个 answer ,我在其中更新了所有包含:'oldRed的嵌套数组字段'.但是我的最终目标是基于嵌套数组字段来更新其他字段 picture .

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
}
});

推荐答案

请尝试以下操作:

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
        }
   )

收藏数据:

{
    "_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"
                }
            ]
        }
    ]
}

结果:

/* 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"
                }
            ]
        }
    ]
}

这篇关于MongoDB-根据嵌套数组的字段值更新数组对象中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆