Querying on bson fields in Mongo DB using the native mongodb module for node

I have a mongo collection which has been filled with documents by a program using the Mongo DB C# driver.

If I run a find

client.connect('mongodb://127.0.0.1:27017/foo', function(err, db) {
  var things = db.collection('things');
  things.find({}, ['ThingId']).limit(1).toArray(function(err, docs) {
    console.log(docs[0]);  
  }
}

and look at what is stored then I see something like

{ _id: 1234235341234, ThingID: { _bsontype: 'Binary', sub_type: 3, position: 16, buffer: <Buffer a2 96 8d 7f fa e4 a4 48 b4 80 4a 19 f3 32 df 8e> }}

I've read the documentation and tried things like:

console.log(mongojs.Binary(docs[i].SessionId.buffer, 3).value());

but I can't print the ThingId as a UUID string to the console

and I definitely can't query on it!

My goal is to query by passing in the GUID strings to find so I can select documents using Ids I know the C# generated (and can see using RoboMongo)

Any help appreciated hugely!


Update: As pointed out by @wes-widner the mongo c# driver team have a UUID helper js file that helps convert between different UUIDs and we use that in RoboMongo to query directly. But the BinData which it uses is only available at the mongo shell and I'm not aware of how to access it using node.

The linked answer shows how to query using uuidHelper and BinData when using the mongo shell essentially what Im asking is how to do that within node

Not really sure if this is what you are looking for but it was what I was looking for when I got to this page. I have java.util.UUID/fromString created UUIDs as primary keys in Mongo and I want to use the normal string UUID in the UI. I am using node-mongodb-native.

var Binary = require('mongodb').Binary;
var toUUID, toBinData;

module.exports.toUUID = toUUID = function(binId) {
  var hex = binId.toString('hex');
  return 
    hex.substr(0, 8) + '-' + 
    hex.substr(8, 4) + '-' + 
    hex.substr(12, 4) + '-' + 
    hex.substr(16, 4) + '-' + 
    hex.substr(20, 12);
};

module.exports.toBinData = toBinData = function(uuid) {
  var buf = new Buffer(uuid.replace(/-/g, ''), 'hex');
  return new Binary(buf, Binary.SUBTYPE_UUID_OLD);  
};

Update

It turned out that while the above works just fine (since it does the conversion similarly to both ways) it does not produce the same string UUID what I see in my Clojure code. But the same uuidhelpers to the rescue - below works for the Java legacy UUIDs.

var Binary = require('mongodb').Binary;
var toJUUID, toBinData;

module.exports.toJUUID = toJUUID = function(binId) {
  var hex = binId.buffer.toString('hex');
  var msb = hex.substr(0, 16);
  var lsb = hex.substr(16, 16);
  msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
  lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
  hex = msb + lsb;
  return hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
};

module.exports.toBinData = toBinData = function(uuid) {
  var hex = uuid.replace(/[{}-]/g, "");
  var msb = hex.substr(0, 16);
  var lsb = hex.substr(16, 16);
  msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
  lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
  hex = msb + lsb;
  return new Binary(new Buffer(hex, 'hex'), Binary.SUBTYPE_UUID_OLD);
};

Following the same copy/paste method you can strip working C# code from the helpers. You only need to handle the buffers a little different.

It looks like this question may be related to your question. And it appears that this library could help you out in converting between binary types produced by the C# driver.