【问题标题】:mongodb query required to retrieve and delete 1000 records in a single query [duplicate]mongodb查询需要在单个查询中检索和删除1000条记录[重复]
【发布时间】:2017-08-08 19:35:21
【问题描述】:

我想使用 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。它执行速度很快,但删除了工作缓慢的记录。

【问题讨论】:

  • 它实际上并不重复我需要的是单个查询我想高效地读取和删除 1000 条记录

标签: node.js mongodb


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2011-01-10
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多