How can you measure the space that a set of documents (in bytes) occupies in a mongo db?

What I would like to do is figure out how much space in bytes a certain set of documents takes up. For example. something like:

collection.stuff.stats({owner: someOwner}, {sizeInBytes: 1})

Where the first parameter is the query and the second is as the projection of the statistic you want to compute.

I read that there is a function bsonsize

that you can use to measure the size of a single document. I am wondering if I can use this along with aggregation methods to calculate the size of a search. But if I'm going to do this, I want to know how bsonsize works. How it works? Is it expensive to work with?

Are there other parameters for measuring the size of data in mango?

+3


source to share


1 answer


One possible "quick and dirty" way to find this would be to assign your results to a cursor and then insert that result into a new collection and call db.collection.stats

on it. It would look like this in the shell:

var myCursor = db.collection.find({key:value});
while(myCursor.hasNext()) {
    db.resultColl.insert(myCursor.next())
}

db.resultColl.stats();

      



Which should return information about a subset of documents

+1


source







All Articles