如何在mongo中执行整个集合中的更新

时间:2017-02-25 08:02:20

标签: node.js mongodb

我的mongo db中的数据格式为:

{ 
    "_id" : ObjectId("586aac4c8231ee0b98458045"), 
    "store_code" : NumberInt(10800), 
    "counter_name" : "R.N.Electric", 
    "address" : "314 khatipura road", 
    "locality" : "Khatipura Road (Jhotwara)", 
    "pincode" : NumberInt(302012), 
    "town" : "JAIPUR", 
    "gtm_city" : "JAIPUR", 
    "sales_office" : "URAJ", 
    "owner_name" : "Rajeev", 
    "owner_mobile" : "9828024073", 
    "division_mapping" : [
        {
            "dvcode" : "cfc", 
            "dc" : "trade", 
            "beatcode" : "govindpura", 
            "fos" : {
                "_id" : ObjectId("586ab8318231ee0b98458843"), 
                "loginid" : "9928483483", 
                "name" : "Arpit Gupta", 
                "division" : [
                    "cfc", 
                    "iron"
                ], 
                "sales_office" : "URAJ", 
                "gtm_city" : "JAIPUR"
            }, 
            "beat" : {
                "_id" : ObjectId("586d372b39f64316b9c3cbd7"), 
                "division" : {
                    "_id" : ObjectId("5869f8b639f6430fe4edee2a"), 
                    "clientdvcode" : NumberInt(40), 
                    "code" : "cfc", 
                    "name" : "Cooking  & Fabric Care", 
                    "project_code" : "usha-fos", 
                    "client_code" : "usha", 
                    "agent_code" : "v5global"
                }, 
                "beatcode" : "govindpura", 
                "sales_office" : "URAJ", 
                "gtm_city" : "JAIPUR", 
                "active" : true, 
                "agency_code" : "v5global", 
                "client_code" : "USHA_FOS", 
                "proj_code" : "usha-fos", 
                "fos" : {
                    "_id" : ObjectId("586ab8318231ee0b98458843"), 
                    "loginid" : "9928483483", 
                    "name" : "Arpit Gupta", 
                    "division" : [
                        "cfc", 
                        "iron"
                    ], 
                    "sales_office" : "URAJ", 
                    "gtm_city" : "JAIPUR"
                }
            }
        }
    ], 
    "distributor_mail" : "sunil.todi@yahoo.in", 
    "project_code" : "usha-fos", 
    "client_code" : "usha", 
    "agent_code" : "v5global", 
    "distributor_name" : "Sundeep Electrical"
}

我想用以下格式的数据更新整个集合:

{ store_code: '10899',
    counter_name: 'Hariom Electricals',
    address: 'Opp-Durga Marrige Garden,Ram Nagar Ext.Sodala Jaipur',
    locality: 'Sodala',
    pincode: '302019',
    town: 'JAIPUR',
    gtm_city: 'JAIPUR',
    sales_office: 'URAJ',
    owner_name: 'Mr.Rambabu',
    owner_mobile: '9929607288',
    distributor_name: 'Ajanta Electricals',
    distributor_mail: 'ajantaelectricals@live.com'
}

只应根据store_code值更新上述字段。就像应该更新store_code与我的JSON(要插入)匹配的文档,如果我的json有一个新的store_code,那么它应该插入该文件,因为它是(现在我知道我必须将upsert设置为true才能实现此目的但我无法找到对此的查询。)我正在尝试的方法:

 /* db.mdb.collection('counters').update(
        {},{$set:finalArr},
        { upsert: true },{ multi: true },
        function (err, data) {
            if (err) {
                console.log(err)
            }
            else{
                app.send(req,res,data);
            }
        })*/  

2 个答案:

答案 0 :(得分:0)

您正在寻找的是updateMany

答案 1 :(得分:0)

db.collection.updateMany(filter, update, options)可能就是你要找的东西

根据filter更新集合中的多个文档。

finalArr.forEach(obj => {
    db.mdb.collection('counters').updateMany(
        { "store_code": obj.store_code }, { $set: obj },
        { upsert: true }, { multi: true },
        function (err, data) {
            if (err) {
                console.log(err)
            }
            else {
                app.send(req, res, data);
            }
        })
})

使用async我想它可能类似于

var async = require('async');

function updateMany(finalArr, callback) {
    async.each(finalArr, function (obj, cb) {
        db.mdb.collection('counters').updateMany(
            { "store_code": obj.store_code }, { $set: obj },
            { upsert: true }, { multi: true },
            function (err, data) {
                // if (err) {
                //     return cb(err);
                // }
                // else {
                //   //  app.send(req, res, data);
                // }
                cb();
            })
    }, (err) => {
        if (err) {
            return callback(err);
        } else {
            return(null,'updated');
        }
    })
}

updateMany(array, (err, data) => {
    console.log(err, data)
})