使用对象中另一个字段的值更新对象字段的MongoDB数组...?

时间:2016-03-04 05:29:00

标签: mongodb mongoose

想要从mongo中的现有对象字段值更新对象字段数组..以下是我的收藏..

{
    "_id": ObjectId(),
    "sku": "V4696-DR-V33",
    "options": [
        {
            "sku": "8903689984338",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 8
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 10
                }
            ]
        },
        {
            "sku": "1742564789",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 4
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 6
                }
            ]
        },
    ]
}

从上面的集合中想要更新上面集合的库存options.retailer.id = 101011然后通过 options.retailer.inventory &的差异更新 options.retailer.inventory options.retailer.buffer_quantity 然后输出如下所示

{
    "_id": ObjectId(),
    "sku": "V4696-DR-V33",
    "options": [
        {
            "sku": "8903689984338",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 7
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 10
                }
            ]
        },
        {
            "sku": "1742564789",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 3
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 6
                }
            ]
        },
    ]
}

0 个答案:

没有答案