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.
-
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?Omar Bahareth– Omar Bahareth2018-09-14 11:36:07 +00:00Commented 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.B..– B..2018-09-14 16:09:15 +00:00Commented Sep 14, 2018 at 16:09
Add a comment
|
1 Answer
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