-1

I encountered a strange behavior of mongo and I would like to clarify it a bit. I wanted practically find out how many bytes does a single document consume when it has only one boolean attribute present in.

Using Stackoverflow, I found out that I can use the following command

Object.bsonsize - some javascript method that should return a size in bytes

While Executing these commands I got some results which is confusing.

Here I provide with the steps I incorporated

  1. I Created a Database called mydatabase and stored a collection called datarandom which has one attribute called status and I have set it as false.It was assigned an Object Id=558bf45d5ea9019aec35d7a2. I ran the following query in RoboMongo

    Object.bsonsize(db.datarandom.find( {"_id"  :ObjectId("558bf45d5ea9019aec35d7a2")})); 
    

    I got 100 bytes.

  2. I Ran the second Query on RoboMongo Which is as follows

    Object.bsonsize(db.datarandom.findOne( {"_id" :ObjectId("558bf45d5ea9019aec35d7a2")})); 
    

    I got the output as 31 bytes only!!!!!!

Can Somebody Explain why I am getting two different outputs when I use Find and FindOne when there is only one Document in my Collection with only boolean as an attribute.

shubhamagiwal92
  • 1,362
  • 4
  • 25
  • 47
  • Object.bsonsize(db.datarandom.find( {"_id" :ObjectId("558bf45d5ea9019aec35d7a2")})).. - gives size of the cursor. | Object.bsonsize(db.datarandom.findOne( {"_id" :ObjectId("558bf45d5ea9019aec35d7a2")}))... - gives size of the doc – somspeaks Oct 11 '17 at 19:07

2 Answers2

1

There are multiples ways to find out the collection size or document size.

In your scenario you inserted only one document with one field status, So two fields in one document _id and status

Use the following command

 use mydatabase  //To use particular database

 db.datarandom.stats() //Returns information of datarandom collection

Size gives the documents size

Count gives no of documents, etc.

More details about the collection stats link

Edit

Note:

find - which returns cursor object

Note: Cursor- A pointer to the result set of a query.

findOne - returns particular document, So its gives perfect size of the document without padding space while using with db.bsonsize.

collection.stats() - Which returns details about the collection, size field gives the document size including padding space.

Note: Every document in MongoDB is stored in a record which contains the document itself and extra space, or padding. Padding allows the document to grow as the result of updates while minimizing the likelihood of reallocations.

Check for storage characteristics

Big Note

Even though you have only one document or many, find and findone behavior won't change.

First understand where to use what.

One question for you

For example:

   [{status: true}] and {status :true} 

is same? whether it gives same size?

Community
  • 1
  • 1
karthick
  • 5,998
  • 12
  • 52
  • 90
  • 1
    hi Karthik, when i ran that command I am getting the Size as 48 bytes. Which is the correct one stats() or object.bsonsize? – shubhamagiwal92 Jun 26 '15 at 05:29
  • @shubhamagiwal +1 for pointing out, Plz check the edit, Choose the appropriate method based on your requirement – karthick Jun 26 '15 at 06:12
  • hi I have only one document in the collection so effectively find and findone should give me the same size. Can you please explain why is there a difference even when find gives a Cursor? – shubhamagiwal92 Jun 26 '15 at 07:23
  • 1
    kudos @karthick.k . This is what i expected. – gauti Jun 26 '15 at 07:49
0

The db.collection.find() query returns a cursor to the selected document and the db.collection.findOne() query returns one document, so the sizes are different between a cursor and for one document.

To access the documents with db.collection.find(), you need to iterate over the cursor with .next() or .toArray()

  • 1
    hi I have only one document in the collection so effectively find and findone should give me the same size. Can you please explain why is there a difference even when find gives a Cursor? – shubhamagiwal92 Jun 26 '15 at 05:27
  • 1
    "should give the same size" - should? I think not. In one case you get size of a single document. In the other, you're getting size of a *cursor*. You're comparing incomparable things. – Sergio Tulentsev Jun 26 '15 at 08:05