3

I'm fetching a document using db.coll.findOne. I wanted to get size of the document in bytes using nodejs with only mongo-native driver.can it be possible?

2
  • 1
    Maybe stackoverflow.com/questions/22008822/… ? Commented Jan 13, 2016 at 13:03
  • 1
    "Object.bsonsize" execute only in mongoshell i need to get size of document in node.js application Commented Jan 13, 2016 at 13:28

1 Answer 1

5

It is possible using BSON (it's a dependency of the mongodb driver):

var bson = require("bson");
var BSON = new bson.BSONPure.BSON();

db.coll.findOne(query).then(function(err, doc) {
  var size = BSON.calculateObjectSize(doc);
});
Sign up to request clarification or add additional context in comments.

3 Comments

i was able to get size values.but it differs from "Object.bsonsize" value is there any reason for the difference in size?
I don't know why exactly but a guess: Object.bsonsize() returns the true size of the stored document, calculateObjectSize() will only try to calculate it (the name is a good indicator :p) so anything thing can lead to a different calculated size.
I had to change it to new bson.BSON() to get it working. I guess the api changed since 2013.

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.