Dheeraj
Dheeraj

Reputation: 556

Mongo Db collection size using conditions

I am using mongo db for my dashboard project. Now i have to study the limitations of my project. My schema for this is is as follows :

 {
    "_id" : ObjectId("5388796faff25d43f4b07c62"),
    "users" : 4611679,
    "shouts" : 12821459,
    "date" : ISODate("2014-01-07T00:00:00Z"),
    "user" : "52a82b20701054a10a000f12",
    "events" : 0,
    "profile" : 0,
    "appName" : "mukund"
 }

the field 'appName' can take many values. I am using the mongo db command: db.collectionn.stats() to see the memory size of the collection. Now the problem is that i want to know the size of the collections which are having appName as 'mukund', it means i want to filter the appName then apply the command on the filtered result but i am unable to do it using the mongo db command 'db.collection.stats()'. Can anyone suggest me some other ways. Thank you......

Upvotes: 2

Views: 389

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59571

Use the following to calculate the total size:

var totalSize = 0;
db.test.find({appName: 'mukund'}).forEach(function(obj) {
    var size = Object.bsonsize(obj); 
    totalSize += size;
})
print(totalSize); //prints size in bytes

Upvotes: 2

Related Questions