How to stream MongoDB Query Results with nodejs?

前端 未结 5 685
名媛妹妹
名媛妹妹 2020-12-23 14:33

I have been searching for an example of how I can stream the result of a MongoDB query to a nodejs client. All solutions I have found so far seem to read the query result at

5条回答
  •  有刺的猬
    2020-12-23 14:45

    Here is the solution I found (please correct me anyone if thatis the wrong way to do it): (Also excuse the bad coding - too late for me now to prettify this)

    var sys = require('sys')
    var http = require("http");
    
    var Db = require('/usr/local/src/npm/node_modules/mongodb/lib/mongodb').Db,
      Connection = require('/usr/local/src/npm/node_modules/mongodb/lib/mongodb').Connection,
      Collection = require('/usr/local/src/npm/node_modules/mongodb/lib/mongodb').Collection,
      Server = require('/usr/local/src/npm/node_modules/mongodb/lib/mongodb').Server;
    
    var db = new Db('test', new Server('localhost',Connection.DEFAULT_PORT , {}));
    
    var products;
    
    db.open(function (error, client) {
      if (error) throw error;
      products = new Collection(client, 'products');
    });
    
    function ProductReader(collection) {
            this.collection = collection;
    }
    
    ProductReader.prototype = new process.EventEmitter();
    
    ProductReader.prototype.do = function() {
            var self = this;
    
            this.collection.find(function(err, cursor) {
                    if (err) {
                            self.emit('e1');
                            return;
    
                    }
                    sys.puts("Printing docs from Cursor Each");
    
                    self.emit('start');
                    cursor.each(function(err, doc) {
                            if (!err) {
                                    self.emit('e2');
                                    self.emit('end');
                                    return;
                            }
    
                            if(doc != null) {
                                    sys.puts("doc:" + doc.name);
                                    self.emit('doc',doc);
                            } else {
                                    self.emit('end');
                            }
                    })
            });
    };
    http.createServer(function(req,res){
            pr = new ProductReader(products);
            pr.on('e1',function(){
                    sys.puts("E1");
                    res.writeHead(400,{"Content-Type": "text/plain"});
                    res.write("e1 occurred\n");
                    res.end();
            });
            pr.on('e2',function(){
                    sys.puts("E2");
                    res.write("ERROR\n");
            });
    
            pr.on('start',function(){
                    sys.puts("START");
                    res.writeHead(200,{"Content-Type": "text/plain"});
                    res.write("\n");
            });
    
            pr.on('doc',function(doc){
                    sys.puts("A DOCUMENT" + doc.name);
                    res.write("" + doc.name + "\n");
            });
    
            pr.on('end',function(){
                    sys.puts("END");
                    res.write("");
                    res.end();
            });
    
            pr.do();
    
      }).listen(8000);
    

提交回复
热议问题