更新Mongo集合中具有相同名称的字段

时间:2018-09-11 21:52:50

标签: json node.js mongodb mongoose

我的问题是,我有一个这样的收藏集:

"cost_price" : 79.9,
"list_price" : 189.9,
"sale_price" : 189.9,
"integrations" : {
    "erp" : {
        "mbm" : {
            "cost_price" : 79.9,
            "list_price" : 189.9,
            "sale_price" : 189.9
        },
        "linx" : {
            "cost_price" : 79.9,
            "list_price" : 189.9,
            "sale_price" : 200.1
        }
    },
    "marketplace" : {
        "netshoes" : {
            "list_price" : 189.9,
            "sale_price" : 199.9
        }
    },
    "ecommerce" : {
        "vtex" : {
            "list_price" : 189.9,
            "sale_price" : 189.9
        },
        "magento" : {
            "list_price" : 189.9,
            "sale_price" : 189.9
        }
    }
}

如何通过单个查询更新此集合中所有名为“ list_price”的字段?

1 个答案:

答案 0 :(得分:0)

可能会帮助您。

db.products.updateMany({}, {
 $rename: {
    "list_price": "new_name",
    "integrations.erp.mbm.list_price": "new_name",
    "integrations.erp.linx.list_price": "new_name",
    "integrations.erp.linx.list_price": "new_name",
    "integrations.marketplace.netshoes.list_price": "new_name",
    "integrations.ecommerce.vtex.list_price": "new_name",
    "integrations.ecommerce.magento.list_price": "new_name"        
 }})