MongoError:池正在排空禁止的新操作 [英] MongoError : pool is draining new operations prohibited

查看:146
本文介绍了MongoError:池正在排空禁止的新操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时出现错误:

Hi I am getting error when i run the following code :

var MongoClient = require('mongodb').MongoClient
var url = "mongodb://localhost:27017/odpLogs"
MongoClient.connect(url,function(err , db){
           if(err) throw err ;
           dao = db.db("odpLogs");
        dao.listCollections({},{nameOnly : true}).toArray(function(err,collectionNames){
            if(err) throw err ;
            collectionNames.forEach( collection => {
                if(collection.name.includes('.logs')){
                    var newvalues   = { $set: { logType: "api" , app : collection.name.split('.')[0]  } };
                    var renameKey = {$rename : {"method" : "operation"}} ;
                    dao.collection(collection.name).updateMany({},newvalues, function(err ,res ){
                        if(err) throw err ;
                        console.log(res);
                    }) ;
                    dao.collection(collection.name).updateMany({},renameKey, function(err ,res ){
                        if(err) throw err ;
                        console.log(res);
                    }) ;
                }
            });
        });
           db.close();

});

我正在尝试将键方法"重命名为操作",为集合中以 .logs 结尾的所有文档添加新的键 logType 和 app.

I am trying to rename key "method" to "operation", add new key logType and app for all document in collection which ends with .logs.

我收到以下错误:

   throw err;
      ^

MongoError: pool is draining, new operations prohibited
    at Pool.write (/home/meghana/Documents/Migration/node_modules/mongodb/lib/core/connection/pool.js:845:8)
    at _command (/home/meghana/Documents/Migration/node_modules/mongodb/lib/core/wireprotocol/command.js:128:10)

推荐答案

foreach 不会等待每个 db 调用完成.它把它们全部踢掉,你最终会使用你的游泳池.

foreach does not wait for each db call to complete. It kicks them all off and you end up using your pool.

尝试使用 for 循环:

for (const item of feed.items) {...}

如果你在 for 循环中使用 awaits,它实际上会等待每个完成而不是耗尽池.

If you use awaits in the for loop it will actually wait for each to complete and not drain the pool.

这篇关于MongoError:池正在排空禁止的新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆