我有这个:
d.db(dbName)
.collection(dbCollName)
.find()
.sort({_id: 1})
.limit(5000).stream()
此表达式返回此:
基本上我要做的就是代替这个:
d.db(dbName)
.collection(dbCollName)
.find()
.sort({_id: 1})
.limit(5000).toArray().then(console.log);
相反,如果可能的话,我想逐一流式传输文档。
是,由cursor
返回的mongodb db.collection().find()
确实具有stream() api
以流式传输文档:
stream() api
有时返回使用const stream = db
.collection("dbCollName")
.find()
.stream();
stream.on("data", function(data) {
//get streamed documents one by one
const currentDoc = data;
});
stream.on("error", function(error) {
console.error("STREAM ERROR::", error.stack);
});
stream.on("end", function() {
console.info("Streaming docs finished");
client.close(); //new MongoClient(url, { useNewUrlParser: true });
});
光标为stream
编写了一个演示测试,以克隆大量数据集的集合。 mongodb node driver