简体   繁体   English

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

[英]MongoDB get the number of documents (count) in a collection using Node.js

I'm currently writing a function that should return the number of documents from a collection the thing is that when i'm returning the value it says undefined, Here is my code:我目前正在编写一个函数,该函数应该返回集合中的文档数,当我返回值时,它表示未定义,这是我的代码:

    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);

Please help me.请帮我。

Here is how it should look like这是它应该是什么样子

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);
        });
    }); 
} 

And usage和用法

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

Keep in mind if you are going to call this function lots of times, it is better to just connect to the database once and then use the same connection.请记住,如果您要多次调用此函数,最好只连接到数据库一次,然后使用相同的连接。

The use of count() is deprecated since mongodb 4.0.自 mongodb 4.0 起不推荐使用count()

Instead you should use documentCount() alternative, but is much moere slower than count() with large ammounts of data.相反,您应该使用documentCount()替代方案,但比具有大量数据的count()慢得多。

The alternatives are estimatedDocumentCount() that performs quite well and the usage is:替代方案是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);
        });
    }); 
} 

Another option I use is to get the number of documents from the collection stats :我使用的另一个选项是从集合 stats 中获取文档数:

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);
        });
    }); 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM