Node.js 如何在mongodb nodejs驱动程序3.0中使用游标?

Node.js 如何在mongodb nodejs驱动程序3.0中使用游标?,node.js,mongodb,cursor,Node.js,Mongodb,Cursor,使用mongodb nodejs驱动程序版本2,我可以执行以下操作: const cursor = db.collection('names').find() 然后按如下方式迭代: cursor.forEach((doc)=> { console.log(doc) }, err=> { client.close() }) 不知何故,在版本3中,光标看起来不可编辑。 我做错什么了吗 编辑:完整代码 const MongoClient = require('mongo

使用mongodb nodejs驱动程序版本2,我可以执行以下操作:

const cursor = db.collection('names').find()
然后按如下方式迭代:

cursor.forEach((doc)=> {
    console.log(doc)
}, err=> {
    client.close()
})
不知何故,在版本3中,光标看起来不可编辑。 我做错什么了吗

编辑:完整代码

const MongoClient = require('mongodb').MongoClient
const assert = require('assert')
MongoClient.connect('mongodb://localhost:27017/db_name', { useNewUrlParser: true }, (err, client)=> {
    assert.equal(null, err)
    const db = client.db()
    const cursor = db.collection('names').find()
    cursor.forEach(doc=>{
        // Somehow, the code doesn't seem to enter this block of code
        console.log(doc)
    }, err => {
       // but later enters here to close the database connection.
       client.close()
    })
})

尝试使用
集合('names')。查找({})。toArray((错误,文档)=>{…
@AlejandroMontilla我正在尝试使用游标。您的建议将在显示之前获取内存中的所有数据,这与游标的操作方式完全不同。是的,但您可以先尝试一下,以检查是否接收到文档,如果没有,则您确定问题不在每个
AlejandroMo中是的,我现在明白你说的了。
collection('names')。find({})。toArray((错误,文档)=>{…
工作正常并返回数据。我尝试了你的代码,它工作正常。你有什么错误/输出?