读取mongodb游标产生 - MongoError:clientcursor已经在使用?

时间:2017-10-12 15:03:14

标签: node.js mongodb

MongoError:clientcursor已经在使用?司机问题?

const { MongoClient } = require('mongodb');

(async () => {
  const db =  await MongoClient.connect('mongodb://127.0.0.1/test');
  const cursor = db.collection('test').find();

  while (cursor.hasNext()) {
    const item = await cursor.next();
    console.log(item);
  }
})().catch(console.error);

产生以下错误:

{ MongoError: clientcursor already in use? driver problem?
    at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
    at /node_modules/mongodb-core/lib/connection/pool.js:497:72
    at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:443:16)
    at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:477:5)
    at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:331:22)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)
    at readableAddChunk (_stream_readable.js:253:11)
    at Socket.Readable.push (_stream_readable.js:211:10)
  name: 'MongoError',
  message: 'clientcursor already in use? driver problem?',
  ok: 0,
  errmsg: 'clientcursor already in use? driver problem?',
  code: 12051,
  codeName: 'Location12051' }

版本:

  • mongodb@2.2.31
  • mongodb-core@2.1.15
  • node v8.6.0

有人解决了这个问题吗?

1 个答案:

答案 0 :(得分:1)

我错了......我忘了await cursor.hasNext()

正确的代码是:

while (await cursor.hasNext()) {
  const item = await cursor.next();
  console.log(item);
}