Getting Collection Information

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.

Listing 13.6 collection_stat.js: Retrieving and displaying the stats for a collection


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


Image

Figure 13.6 Retrieving and displaying the stats for a collection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset