How can I fetch binary data from mongoDB in node.js ?

795 views
Skip to first unread message

Natalie

unread,
Feb 4, 2011, 9:42:03 AM2/4/11
to node-mongodb-native
How can I fetch binary data from mongoDB in node.js ?

db.collection('photos', function(err, collection) {
collection.find({'_id': '123456'}, function(err, cursor) {
cursor.toArray(function(err, items) {
..........................
});
});
});

this code not working, the server hangs up.

maga

unread,
Feb 5, 2011, 9:42:00 AM2/5/11
to node-mong...@googlegroups.com
If you really need to work with binary data, you should use GridFS for storing binary data in MongoDB, and so you should use GridFS functionality of node-mongodb-native to fetch or to do whatever: http://goo.gl/WIC48

Natalie

unread,
Feb 5, 2011, 5:04:57 PM2/5/11
to node-mongodb-native
My data is less then 4Mb, I dont want to use GridFS.
I dont have problems with reading binary fields in C++ and php.
Maybe there's some kind of trick.
Please help me.

Natalie

unread,
Feb 5, 2011, 5:09:37 PM2/5/11
to node-mongodb-native
in case of 256KB chanks, looks like GridFS makes 5 disk reads to get
1MB photo. this is not what would I expect from the best performing
server.
in our case we can even optimize majority of photos to under 256KB
size, but node-mongodb-native does not allow me to read this from
MongoDB with one disk read and without overhead of having second
table. Is it "designed drawback", or I'm doing something wrong?

Natalie

unread,
Feb 6, 2011, 3:58:23 PM2/6/11
to node-mongodb-native
diff ./node/lib/mongodb/bson/bson.js ./node/lib/mongodb/bson/
bson.js.orig
303,306c302,303
< // var number_of_bytes = BinaryParser.toInt(data.substr(index,
4));
< // index = index + 4;
< var number_of_bytes=total_number_of_bytes;
---
> var number_of_bytes = BinaryParser.toInt(data.substr(index, 4));
> index = index + 4;

It helped me.
Reply all
Reply to author
Forward
0 new messages