Tuesday 30 May 2017

How to Improve the Performance of mongodb long running Query

I am new to mongoDb, i am trying to update the fields for Each Records for around 10 to 15k records. When i am trying to update the records the query blocks whole database, the Running query will allow me to do any read or write operations till the Query Execution completes, is there anyway to improve the performance for this kind of Queries.

Here is My Code:

var ExisitingData=[{"isActive" : true, 
    "barcode" : "8908001921015",     
    "mrp" : 2000, 
},
{"isActive" : true, 
    "barcode" : "7808001921019",     
    "mrp" : 1000, 
}
....15k]
var updatedRsult=[];
    async.forEach(ExisistingData, function (item, innerCallback) {
        exports.populateData(item, function (err, populatedResult) {
            if (err) {
                innerCallback(err);
            }
            if(populatedResult==true)
                totalRecordsUpdated++;
            else
                totalIgnoredRecords++;
            innerCallback();

        });
    }, function (err) {
        console.log("FinalDone");
        var h1={}
        h1['totalRecordsUpdated'] = totalRecordsUpdated;
        h1['totalIgnoredRecords'] = totalIgnoredRecords;
        updatedResult.push(h1);
        updateCheck(null, updatedResult);
    });




exports.populateData=function(item, mainCallback) {
    var updated = false;
    async.parallel([
        function (callback1) {
            Test.update({
                $and: [
                    {'barcode': item['barcode']},
                    {'mrp': {$lt: parseInt(item['mrp'])}}
                ]
            }, {$set: {'mrp': parseInt(item['mrp'])}}, function (err, result) {
                if (err) {
                    console.log(err);
                    callback1();
                }

                else {
                    if (result['nModified'] == 1) {
                        console.log("Its Updated");
                        console.log(item);

                       updated=true;
                        callback1()
                    }
                    else {
                        callback1()
                    }
                }

            });
        }

    ], function done(err) {
        mainCallback(null,updated);
    });
};



via Jeevan

No comments:

Post a Comment