MongoDB使用Node.js获取集合中的文档数(count)

时间:2014-08-24 16:10:10

标签: node.js mongodb mongoose database

我目前正在编写一个函数,该函数应该返回集合中的文档数量,这就是当我返回未定义的值时,这是我的代码:

    var MongoClient = require('mongodb').MongoClient;


// open the connection the DB server
var dbName = "ystocks";
var port = "27017";
var host = "localhost";
var tt = "mongodb://" + host + ":" + port + "/" + dbName;
//"mongodb://localhost:27017/ystocks"
function getNumOfDocs (collectionName, host, port, dbName) {
    var tt = "mongodb://" + host + ":" + port + "/" + dbName;
    count = 0;
    MongoClient.connect(tt, function (error, db){

        if(error) throw error;
        collectionName = collectionName;
        db.collection(collectionName).count({}, function(error, numOfDocs){
            if (error) throw error;

            //print the result
            console.dir("numOfDocs: " + numOfDocs);
            count = numOfDocs;
            console.log("count is : " + count);

            // close the DB
            return numOfDocs;
            db.close();


        });// end of count

    }); // Connection to the DB
    //return count;

} // end of getNumOfDocs


var ee = getNumOfDocs ("stocks", "localhost", "27017", "ystocks");
console.log("ee is " + ee);

请帮帮我。

2 个答案:

答案 0 :(得分:2)

以下是应该的样子

var MongoClient = require('mongodb').MongoClient;

var dbName = "ystocks";
var port = "27017";
var host = "localhost";

function getNumOfDocs (collectionName, host, port, dbName, callback) {
    MongoClient.connect("mongodb://" + host + ":" + port + "/" + dbName, function (error, db){
        if(error) return callback(error);

        db.collection(collectionName).count({}, function(error, numOfDocs){
            if(error) return callback(error);

            db.close();
            callback(null, numOfDocs);
        });
    }); 
} 

使用

getNumOfDocs("stocks", host, port, dbName, function(err, count) {
   if (err) {
       return console.log(err.message);
   }
   console.log('number of documents', count);
});

请记住,如果要多次调用此函数,最好只连接一次数据库并使用相同的连接。

答案 1 :(得分:0)

从mongodb 4.0开始不推荐使用 count()

相反,您应该使用 documentCount()替代方法,但要比 count()慢得多,并且要处理大量数据。

替代方法是 estimatedDocumentCount(),该方法效果很好,用法是:

var MongoClient = require('mongodb').MongoClient;

var dbName = "ystocks";
var port = "27017";
var host = "localhost";

function getNumOfDocs (collectionName, host, port, dbName, callback) {
    MongoClient.connect("mongodb://" + host + ":" + port + "/" + dbName, function (error, db){
        if(error) return callback(error);

        db.collection(collectionName).estimatedDocumentCount({}, function(error, numOfDocs){
            if(error) return callback(error);

            db.close();
            callback(null, numOfDocs);
        });
    }); 
} 

我使用的另一个选项是从收集统计信息中获取文档数:

var MongoClient = require('mongodb').MongoClient;

var dbName = "ystocks";
var port = "27017";
var host = "localhost";

function getNumOfDocs (collectionName, host, port, dbName, callback) {
    MongoClient.connect("mongodb://" + host + ":" + port + "/" + dbName, function (error, db){
        if(error) return callback(error);

        db.collection(collectionName).stats(function(error, stats){
            if(error) return callback(error);

            db.close();
            callback(null, stats.count);
        });
    }); 
}