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?
-
1Maybe stackoverflow.com/questions/22008822/… ?Shanoor– Shanoor2016-01-13 13:03:36 +00:00Commented Jan 13, 2016 at 13:03
-
1"Object.bsonsize" execute only in mongoshell i need to get size of document in node.js applicationNarayansingh Rajput– Narayansingh Rajput2016-01-13 13:28:15 +00:00Commented Jan 13, 2016 at 13:28
Add a comment
|
1 Answer
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);
});
3 Comments
Narayansingh Rajput
i was able to get size values.but it differs from "Object.bsonsize" value is there any reason for the difference in size?
Shanoor
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.Mr. Goferito
I had to change it to
new bson.BSON() to get it working. I guess the api changed since 2013.