Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - How can I get the size of a collection using Node.js?

I'm writing a server side code using node.js, I'm trying to get the MongoDB collection size using count method which is not working.

This is my code

var mongo = require('mongodb');
var host =  "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;

function getStock(name, callback) {

    var db = new mongo.Db("testDB", new mongo.Server(host, port, {}));
    db.open (function(error){
        console.log("We are connected! " + host + ":" +port);

        db.collection("stocks", function(error, collection){
            console.log("We have a collection");
            **var numOfDocs = db.collection('stocks').count()**
            **console.log("The num of  Docs in our collection is: ",numOfDocs)**
            collection.find({"name":name.toString()}, function(error, cursor) {
                cursor.toArray(function(error, stocks) {
                    if (stocks.length==0) {
                        //console.log("No Stocks found!");
                        callback(false);
                    }
                    else {
                        callback(stocks[0]);
                        //console.log("Found a stock -> ",stocks[0]);
                    }
                });
            });


        });
    });
}
like image 581
Alex Brodov Avatar asked Aug 18 '14 21:08

Alex Brodov


People also ask

How to get the size of a collection using MongoDB?

How to get the size of a collection using Node.js ? The collection.countDocuments () is the method of the mongodb module in the node.js that is used to count the total number of documents present in the collection of the particular database in the MongoDB. Parameters: This method takes the following one parameter which is not required by default:

How to get the size of a collection using Node JS?

- GeeksforGeeks How to get the size of a collection using Node.js ? The collection.countDocuments () is the method of the mongodb module in the node.js that is used to count the total number of documents present in the collection of the particular database in the MongoDB.

What is the use of bsonsize in MongoDB?

The $bsonSize aggregation pipeline operator was introduced in MongoDB 4.4 for the purpose of returning the size of a BSON document. You can use $bsonSize to return the total size of all documents in the collection by combining it with the $group and $sum operators.

What is the size of the database collection in MySQL?

It’s only a small collection with a handful of small documents, and so the size is only 3012 bytes. The db.collection.totalSize () method returns the total size in bytes of the data in the collection plus the size of every index on the collection. Therefore, use this method if you want to know the total size that includes all indexes.


1 Answers

.count() is an asynchronous function, just like .find() is.

Try the following code:

collection.count({}, function(error, numOfDocs) {
    console.log('I have '+numOfDocs+' documents in my collection');
    // ..
});

The first argument to .count() should be a search query. Since you want to count all documents, I'm passing an empty object {} as a search query.

like image 83
Leonid Beschastny Avatar answered Nov 15 '22 04:11

Leonid Beschastny