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

后端 未结 2 1648
梦谈多话
梦谈多话 2021-01-19 03:42

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

相关标签:
2条回答
  • 2021-01-19 04:24

    The use of count() is deprecated since mongodb 4.0.

    Instead you should use documentCount() alternative, but is much moere slower than count() with large ammounts of data.

    The alternatives are estimatedDocumentCount() that performs quite well and the usage is:

    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:

    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);
            });
        }); 
    } 
    
    0 讨论(0)
  • 2021-01-19 04:30

    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.

    0 讨论(0)
提交回复
热议问题