0

How do I get the BSON document size of a MongoDB record using the Ruby connector? For BSON::Document.new(some_hash), .size seems to return the number of keys for the document, .bson_size doesn't exist, and .data_size returns an error.

2
  • Hey, this might be a possible duplicate of How I know my document's size inside MongoDB with the ruby driver. Could you take a look there and see if the answers there help you out? Commented Sep 14, 2018 at 11:36
  • That page suggests using BSON.serialize, but it doesn't seem to exist. I think it may have been part of a previous version. Commented Sep 14, 2018 at 16:09

1 Answer 1

0

As of Mongo's Ruby Driver 2.0 release, BSON.serialize is removed. If you have a BSON::Document, you can transform it to a BSON::ByteBuffer by calling to_bson, and then get its size by calling length on that.

Example:

BSON::Document.new({a: 1}).to_bson.length
=> 12
Sign up to request clarification or add additional context in comments.

Comments

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.