使用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()
})
})