10

The limitation of one document in MongoDB is 4Mo. I have some documents really heavy with a lot of data.

How I know my document's size inside MongoDB with the ruby driver ?

2 Answers 2

28

Or native Object.bsonsize(db.Foo.findOne());

Sign up to request clarification or add additional context in comments.

2 Comments

+1 ! Can even size up the attribute of the document : Object.bsonsize(db.customer.findOne({'name' : 'harry'}).address);
Moreover, you can check size of any object: Object.bsonsize(ObjectId()), Object.bsonsize({ is_free: true }).
4

You can use BSON.serialize and find the length of the resulting byte buffer. See http://www.mongodb.org/display/DOCS/BSON#BSON-Ruby for an example of using BSON.serialize.

1 Comment

So you can do it like this: BSON.serialize(database.collection(:collection_name).find_one(_id: BSON::ObjectId('4f8698bfc4eb9605a1000001'))).size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.