简体   繁体   中英

How can I get size (bytes) of a MongoDB collection with Mongoose?

Imagine I have a Mongoose model like this:

mongoose = require('mongoose')
Products = mongoose.model('Products')

How do I get size of the full corresponding collection in bytes? I found db.collection.storageSize , but it's nowhere to be found in the Mongoose API docs.

Mongoose doesn't provide an abstraction for this, but you can access the native driver collection methods like stats() from the collection property of a Mongoose Model. That method provides the collection's size as the storageSize property of the results:

Products.collection.stats(function(err, results) {
    console.log(results.storageSize);
});
var stats = Products.stats(function(err, stats) {
  // second parameter stats contains the result from  stats
  var storageSize = stats.storageSize;
});

gets statistics for Products collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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