最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在Mongodb中从集合流式传输文档

运维笔记admin12浏览0评论

如何在Mongodb中从集合流式传输文档

如何在Mongodb中从集合流式传输文档

我有这个:

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

发布评论

评论列表(0)

  1. 暂无评论