1

我有这个:

d.db(dbName)
     .collection(dbCollName)
     .find()
     .sort({_id: 1})
     .limit(5000).stream()

这个表达式返回这个:

enter image description here

基本上我要做的就是代替这个:

   d.db(dbName)
         .collection(dbCollName)
         .find()
         .sort({_id: 1})
         .limit(5000).toArray().then(console.log);

相反,如果可能的话,我想逐个流式传输文档等。

4

1 回答 1

0

cursor是的,返回的mongodbdb.collection().find()确实具有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 });
});

有时回写了一个演示测试,streammongodb node driver光标克隆一个集合以获得相当数量的数据集。更多信息

于 2019-09-28T00:54:09.647 回答