我正在尝试从Mongo查询创建一个简单的高地流
const connectionString = ...
const client = new MongoClient(connectionString);
const records = mongoClient
.db('mydb')
.collection('acollection')
.find({});
const res = await hi(records).tap(_log).collect().toPromise(Promise);
function _log(record) {
console.log(record);
return record;
}
这段代码可以完美地与mongodb (npm包)一起工作<但是在Mongo>= 4.0.0中,它会一遍又一遍地打印undefined
,直到堆用完。但是在Mongo>
我在docker中运行MongoDb,版本图像mongo:4.2.10
查看mongodb 4.0.0变更日志
<<blockquote>流API/h3>光标不再扩展直接可读的,它必须通过调用转换为流cursor.stream(),例如:
const stream = cursor.stream();
stream.on('data', data => console.log(data));
stream.on('end', () => client.close());
解决问题是简单地添加.stream()
最后mongo查询:
const connectionString = ...
const client = new MongoClient(connectionString);
const records = mongoClient
.db('mydb')
.collection('acollection')
.find({})
.stream(); // Change is here
const res = await hi(records).tap(_log).collect().toPromise(Promise);
function _log(record) {
console.log(record);
return record;
}