'如何通过node.js mongodb驱动程序有效地流式传输大型json文档? [英] 'How to stream a large json doc through the node.js mongodb driver efficiently?

查看:55
本文介绍了'如何通过node.js mongodb驱动程序有效地流式传输大型json文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个node.js应用程序,该应用程序通过mongo node.js驱动程序对mongodb数据库进行查询.我想路由到呈现大json文档的链接,但是,出于性能考虑,我想流式传输该文档.

I am writing a node.js app that makes a query to a mongodb database through the mongo node.js driver. I want to route to a link where a large json document is rendered, however, I would like to stream the document for performance considerations.

代码如下:

var express = require("express"),
app = express(),
MongoClient = require("mongodb").MongoClient;

MongoClient.connect("mongodb://localhost:27017/database",   function(err,db) {
  var query = db.collection("manzanas").find({ "_id": "query" })
                .project({ "_id":0 });

  app.get("/link", function(req,res) {
      query.stream().on("data", function(d) { res.json(d); });
      query.stream().on("end", function() { console.log("done"); res.end(); });
  });

});

app.listen(8080, function() {
  console.log("server is on!");
});

问题是,我第一次尝试访问链接时,一切都按预期工作,但是当我尝试刷新页面或从另一个选项卡或浏览器访问路由时,页面尝试时未呈现json文档刷新,好像互联网连接很慢;最终什么也没显示.

The problem is that the first time I try to visit the link everything works as expected, but when I try to refresh the page or access the route from either another tab or browser the json document is not rendered while the page is trying to refresh as though the internet connection was very slow; and eventually nothing is displayed.

我不知道如何解决它,如果我不流式传输文档并仅按原样显示它,我不会遇到任何问题,但是,我想知道如何使它与流一起使用.为了使其再次工作,我必须完全退出node.js流程,但这不是生产模式方案的选项.

I have no clue on how to fix it, if I don't stream the document and just display it as it is I encounter no problems, however, I would like to know how to make it work with a stream. In order to make it work again I have to exit the node.js process completely but that is not an option for a production mode scenario.

我如何才能做到这一点,最好的方法是什么?没有使用express.js的更好的方法吗?任何评论都将受到赞赏.

How can I achieve this and what is the best approach? Is there a better way to do this without using express.js? Any comments are well appreciated.

感谢您的帮助.

推荐答案

执行此操作

  app.get("/link", function(req,res) {
      var query = db.collection("manzanas").find({ "_id": "query" })
                .project({ "_id":0 });
      query.stream().on("data", function(d) { res.json(d); });
      query.stream().on("end", function() { console.log("done"); res.end(); });
  });

即每次您请求并生成查询时都会生成查询.

I.e. generate the query each time you request and stream on it.

这篇关于'如何通过node.js mongodb驱动程序有效地流式传输大型json文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆