Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java driver equivalent for JavaScript shell's Object.bsonsize( doc )?

I was wondering what the Java driver's equivalent to the Mongo JavaScript shell's Object.bsonsize( doc ) method? For example, what is the Java code to perform the following:

bobk-mbp:~ bobk$ mongo
MongoDB shell version: 2.0.4
connecting to: test
PRIMARY> use devices;
switched to db devices
PRIMARY> Object.bsonsize( db.profiles.findOne( { _id: "REK_0001" } ) );
186
PRIMARY> Object.bsonsize( db.profiles.findOne( { _id: "REK_0002" } ) );
218
PRIMARY> 

How do I perform this same basic use case with the MongoDB Java Driver. Its not obvious through the JavaDocs.

like image 716
Bob Kuhar Avatar asked Sep 17 '26 07:09

Bob Kuhar


1 Answers

There's nothing quite as clean as what's available in the shell, but this will work:

DBObject obj = coll.findOne();
int bsonSize = DefaultDBEncoder.FACTORY.create().
        writeObject(new BasicOutputBuffer(), obj));
like image 102
jyemin Avatar answered Sep 20 '26 01:09

jyemin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!