How to get the size of single document in Mongodb?

105,587

Solution 1

In the previous call of Object.bsonsize(), Mongodb returned the size of the cursor, rather than the document.

Correct way is to use this command:

Object.bsonsize(db.test.findOne())

With findOne(), you can define your query for a specific document:

Object.bsonsize(db.test.findOne({type:"auto"}))

This will return the correct size (in bytes) of the particular document.

Solution 2

MAXIMUM DOCUMENT SIZE 16 MiB (source)


If you have version >=4.4 ($bsonSize source)

db.users.aggregate([
  {
    "$project": {
      "size_bytes": { "$bsonSize": "$$ROOT" },
      "size_KB": { "$divide": [{"$bsonSize": "$$ROOT"}, 1000] },
      "size_MB": { "$divide": [{"$bsonSize": "$$ROOT"}, 1000000] }
    }
  }
])

If you have version <4.4 (Object.bsonSize() source)

You can use this script for get a real size:

db.users.find().forEach(function(obj)
{
  var size = Object.bsonsize(obj);
  print('_id: '+obj._id+' || Size: '+size+'B -> '+Math.round(size/(1000))+'KB -> '+Math.round(size/(1000*1000))+'MB (max 16MB)');
});

Note: If your IDs are 64-bit integers, the above will truncate the ID value on printing! If that's the case, you can use instead:

db.users.find().forEach(function(obj)
{
  var size = Object.bsonsize(obj);
  var stats =
  {
    '_id': obj._id, 
    'bytes': size, 
    'KB': Math.round(size/(1000)), 
    'MB': Math.round(size/(1000*1000))
  };
  print(stats);
});

This also has the advantage of returning JSON, so a GUI like RoboMongo can tabulate it!


edit : thanks to @zAlbee for your suggest completion.

Solution 3

The effective amount of space the document will take in the collection will be more than the size of your document because of the Record Padding mechanism.

This is why there is a difference between the outputs of the db.test.stats() and Object.bsonsize(..).

To get the exact size (in bytes) of the document, stick to the Object.bsonsize() function.

Solution 4

With mongodb 4.4 (upcoming), You can use bsonSize operator to get the document size.

db.test.aggregate([
  {
    "$project": {
      "name": 1,
      "object_size": { "$bsonSize": "$$ROOT" }
    }
  }
])

Solution 5

Object.bsonsize(db.test.findOne({type:"auto"})) It gives in bytes.

Share:
105,587

Related videos on Youtube

user1949763
Author by

user1949763

Updated on January 17, 2022

Comments

  • user1949763
    user1949763 over 2 years

    I encountered a strange behavior of mongo and I would like to clarify it a bit...
    My request is simple as that: I would like to get a size of single document in collection. I found two possible solutions:

    • Object.bsonsize - some javascript method that should return a size in bytes
    • db.collection.stats() - where there is a line 'avgObjSize' that produce some "aggregated"(average) size view on the data. It simply represents average size of single document.

    • When I create test collection with only one document, both functions returns different values. How is it possible?
      Does it exist some other method to get a size of a mongo document?

    Here, I provide some code I perform testing on:

    1. I created new database 'test' and input simple document with only one attribute: type:"auto"

      db.test.insert({type:"auto"})
      
    2. output from stats() function call: db.test.stats():

      { 
        "ns" : "test.test",
        "count" : 1,
        "size" : 40,
        "avgObjSize" : 40,
        "storageSize" : 4096,
        "numExtents" : 1,
        "nindexes" : 1,
        "lastExtentSize" : 4096,
        "paddingFactor" : 1,
        "systemFlags" : 1,
        "userFlags" : 0,
        "totalIndexSize" : 8176,
        "indexSizes" : {
              "_id_" : 8176
      },
      "ok" : 1
      

      }

    3. output from bsonsize function call: Object.bsonsize(db.test.find({test:"auto"}))

      481
      
  • user1949763
    user1949763 over 10 years
    Thank you for your reply, in that case, I have another question regarding this issue: suppose I have a collection where documents with long list of identifiers are saved in the form of the list. (identifiers are stored originally in txt-csv file- with size 300 kB; each identifier is 10characters long) When I run bsonsize on such a document the size is even lower than 481. It returns 465. Could you explain to me this situation, please?
  • John Evans
    John Evans over 9 years
    Which size is used to enforce the mongDB document size limitation? Object.bsonsize()?
  • leon
    leon over 8 years
    How to get the size of a list of document with query?
  • Sercan Ozdemir
    Sercan Ozdemir over 8 years
    But of course this code will fetch the document before calculating the size.
  • htm11h
    htm11h over 8 years
    The MongoDB document size is a constraint of the Mongo, this is covered in the manual on their web site, 16MB. I have hit this limit a number of times trying import records.
  • Liberateur
    Liberateur over 7 years
    This not return a goood size :(... But this : stackoverflow.com/a/40993183/3933634
  • Erce
    Erce over 5 years
    This is exactly what i am looking for but it not working maybe related with my mongo version. current one is 3.4 ?
  • PARAMANANDA PRADHAN
    PARAMANANDA PRADHAN over 5 years
    How to get Object.bsonsize, what is the import or required statement?
  • Félix Paradis
    Félix Paradis over 5 years
    Anyone else getting TypeError: Object.bsonsize is not a function ?
  • Liberateur
    Liberateur over 5 years
    Do you have try in mongo shell ? It's work : docs.mongodb.com/manual/reference/mongo-shell/#miscellaneous
  • Sam
    Sam about 5 years
    For anyone else who missed it, you must use findOne instead of find
  • Emmanuel Mtali
    Emmanuel Mtali over 4 years
    Try to explain your answer . .
  • Wernfried Domscheit
    Wernfried Domscheit almost 4 years
    Proper label would be rather 'KiB': Math.round(size/(1024)), 'MiB': Math.round(size/(1024*1024)) (or 'kB': Math.round(size/(1000)), 'MB': Math.round(size/(1000*1000))
  • Luk Aron
    Luk Aron over 3 years
    how to do it in python?
  • Wernfried Domscheit
    Wernfried Domscheit over 2 years
    For anyone else who missed it, you must use findOne or find().toArray() instead of find Using find().toArray() of course gives the size sum of all documents.