实时查询 MongoDB 个文档

Querying a MongoDB documents in real-time

我正在构建一个可以处理大数据的 Web 应用程序。 我将使用 Apache Storm 挖掘 Twitter 数据,随后将它们保存在 MongoDB 数据库中。 同时,这些数据必须通过 Node.js 实时获取并通过 socket.io 发送到我的前端。 有没有办法通过Node.js实时查询MongoDB? 谢谢

我正在使用 mongoDB 做一个项目,我使用 mongodb npm module 实时查询数据库。

首先,我得到了我数据库中 collection 的列表:

  //My server controller page
    var MongoClient = require('mongodb').MongoClient
    , assert = require('assert');

    exports.getCollections = function(req,res){
        mongoose.connection.db.collectionNames(function(err, names) {
            if (err){ 
               console.log(err)
            } else {
                res.status(200).send({collections: names});
            }
        });
    };

在前端,我做了一个 Angular ng-repeat 来列出我的 collection,然后当我点击 collection 名称时,我 运行 以下代码:

      MongoClient.connect(url, function (err, db) {
            assert.equal(null, err);
            var collection = db.collection(req.body.collName);
            collection.find({}).limit(req.body.limit).toArray(function (err, docs) {
                if (err) {
                   console.log(err)
                } else {
                    res.status(200).send({r: docs, count: docs.length});
                    db.close();
                }
            })
        });

这是我的客户端 angular 代码:

 //get the collection list upon page load
  $http.get('collections')
    .success(function(c){
         $scope.collList = c.collections;
    })
    .error(function(err){
         $scope.error = err;
     });


  //function run when collection is selected
 $scope.doFind = function(coll){            
            $scope.collViewing = coll;
            $http.post('/basicfind',{collName: coll,limit:$scope.limiter})
                .success(function(data){
                  $scope.results = data.r;
                  $scope.countOfResults = data.count;
                })
                .error(function(err){
                    $scope.error = err.message;
                });
        };

希望对您有所帮助,如果您需要我分享更多代码,请告诉我