Another very useful feature of the Collection
object is the ability to get the statistics for a particular collection. The statistics can give you an idea of how big the collection is, both in terms of number of documents and size on disk. You may want to add code that periodically checks the stats of your collections to determine whether they need to be cleaned up.
Listing 13.6 shows how to access the stats for a collection by calling the stats()
method on the Collection
object. Figure 13.6 shows the output from the stats()
call.
01 var MongoClient = require('mongodb').MongoClient;
02 MongoClient.connect("mongodb://localhost/", function(err, db) {
03 var newDB = db.db("newDB");
04 newDB.createCollection("newCollection", function(err, collection){
05 collection.stats(function(err, stats){
06 console.log(stats);
07 db.close();
08 });
09 });
10 });