The scenario is as follows:
Model.find().where('something', 'value').exec(callback)
callback
How can I determine the size of those documents on disk?
Approximations are OK, for me at least.
find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.
The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.
Mongoose count() Function This functions basically specifies this query as a count query. Parameters: This function accepts one array parameter i.e. callback function. Return Value: This function returns Query Object.
All callbacks in Mongoose use the pattern: callback(error, result) . If an error occurs executing the query, the error parameter will contain an error document, and result will be null. If the query is successful, the error parameter will be null, and the result will be populated with the results of the query.
You can do this by executing your query as a lean query (to get plain JavaScript objects instead of Mongoose document instances) and then dipping down into the BSON library that's part of the native MongoDB driver and calling calculateObjectSize
:
var bson = mongoose.mongo.BSON;
Model.find().where('something', 'value').lean().exec(function(err, docs) {
var docsBsonSize = bson.calculateObjectSize(docs));
});
It won't give you the size on disk, but it should be a good approximation of the size of BSON docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With