首页 > 解决方案 > 更新 Mongo 集合中同名的字段

问题描述

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

"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”的所有字段?

标签: jsonnode.jsmongodbmongoose

解决方案


可能对你有帮助。

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

推荐阅读