在单个查询中检索和删除1000条记录所需的mongodb查询

时间:2017-08-08 09:39:32

标签: node.js mongodb

我想使用nodejs在单个查询中删除和检索1000条记录。请任何人都可以帮助我。

   queuetable.find({$query:{ Status : 0, CommunityId: { '$in':  objconfig.queryCommunityIds[varCommunityId]}, 
        DateReceived: { $gte: objcommon.timeFormat(date) } },
        $hint:{Status:1,CommunityId:1,DateReceived:1}}, {}, { limit: 1000 }, 
        function(err, queueRecords) {
          callback(splitNotifications(function(notificationDet) {}, queueRecords, notificationCounter, browserCounter));
        })

var splitNotifications = function(callback, queueRecords, notificationCounter, browserCounter) {        
        if(notificationCounter < queueRecords.length) {
            global.queuetable.remove( { _id: queueRecords[notificationCounter]._id }, function(err, removeSuccess) {
                console.log('removed record');  
                if (err){
                    objcommon.mongodberrorlog(objcommon.mongoTime(), err.stack, 'Error In queueOthers queue update');
                //  console.log('queuetable update failed in splitBrowsers.. :: Time :: '+objcommon.mongoTime()+' :: MatriId :: '+queueRecords[notificationCounter].MatriId);
                    process.exit(1);
                }           
            });
            logRecord[queueRecords[notificationCounter].MatriId] = {                    
                "startTime" :0,
                "endTime" :0,
                "logInsertStartTime":0,
                "logInsertEndTime":0,
                "gcmStartTime" :0,
                "gcmEndTime":0,
                "gcmTime":0,
                "totSentTime":0,
                "browserCounter": 0,
                "notificationType":0,
                "DateSent":queueRecords[notificationCounter].DateSent
            };  
            logRecord[queueRecords[notificationCounter].MatriId].startTime = getCurrentTime();  
notificationCounter++;
                callback(splitNotifications(function(notificationDet) {}, queueRecords, notificationCounter, browserCounter));              
        } 
    }

我想使用nodejs在单个查询中删除和检索1000条记录。请任何人都可以帮助我。在上面的代码中,我递归地调用splitNotifications。它快速执行,但删除工作缓慢记录。

1 个答案:

答案 0 :(得分:1)

为什么不在删除调用中使用$in

global.queuetable.remove( { _id: {$in: queueRecords.map((record) => record._id)} }, function(err, removeSuccess) {));