Object.bsonsize dont work correct

Object.bsonsize(db.Likes.find({Id: HexData(3, "dfa2eae902cb324487e01b6ddd9de085")}))
Object.bsonsize(db.Likes.find({Id: HexData(3, "248b85402e4f9e4591ad6375ff76dbef")}))

return to me equals result:
image

but on first document i have nested array with 1 element , second nested array with 5 elements

Hi @alexov_inbox,

The db.collection.find() method returns a cursor which has to be iterated to get the results.

Since it looks like you are trying to compare the size of individual documents, try using db.collection.findOne() instead (which returns a document instead of a cursor):

Object.bsonsize(db.Likes.findOne({Id: HexData(3, "dfa2eae902cb324487e01b6ddd9de085")}))
Object.bsonsize(db.Likes.findOne({Id: HexData(3, "248b85402e4f9e4591ad6375ff76dbef")}))

Alternatively you can iterate the find() cursor using toArray():

Object.bsonsize(db.Likes.find({Id: HexData(3, "dfa2eae902cb324487e01b6ddd9de085")}).toArray())
Object.bsonsize(db.Likes.find({Id: HexData(3, "248b85402e4f9e4591ad6375ff76dbef")}).toArray())

Note: using toArray() will add a few extra bytes to the result (as compared to findOne()) because it is wrapped as an array.

Regards,
Stennie

2 Likes

yes its work ! thx !

it remains to deal with the fundamental problem :upside_down_face:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.