So the accepted answer didn't work for me. I found a snippet of code in this github issue to parse a .net guid into a buffer:
guid-parse.js:
'use strict';
// Maps for number <-> hex string conversion
var _byteToHex = [];
var _hexToByte = {};
for (var i = 0; i < 256; i++) {
_byteToHex[i] = (i + 0x100).toString(16).substr(1);
_hexToByte[_byteToHex[i]] = i;
}
// **`parse()` - Parse a UUID into it's component bytes**
function parse(s, buf, offset) {
const i = (buf && offset) || 0;
offset = i;
let ii = 0;
buf = buf || Buffer.alloc(16 + i);
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
if (ii < 16) { // Don't overflow!
buf[i + ii++] = _hexToByte[oct];
}
});
// Zero out remaining bytes if string was short
while (ii < 16) {
buf[i + ii++] = 0;
}
// Endian-swap hack...
var buf2 = Buffer.from(buf);
buf[offset+0] = buf2[offset+3];
buf[offset+1] = buf2[offset+2];
buf[offset+2] = buf2[offset+1];
buf[offset+3] = buf2[offset+0];
buf[offset+4] = buf2[offset+5];
buf[offset+5] = buf2[offset+4];
buf[offset+6] = buf2[offset+7];
buf[offset+7] = buf2[offset+6];
return buf;
}
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
function unparse(buf, offset) {
let i = offset || 0;
// Endian-swap hack...
var buf2 = Buffer.from(buf);
buf[i+0] = buf2[i+3];
buf[i+1] = buf2[i+2];
buf[i+2] = buf2[i+1];
buf[i+3] = buf2[i+0];
buf[i+4] = buf2[i+5];
buf[i+5] = buf2[i+4];
buf[i+6] = buf2[i+7];
buf[i+7] = buf2[i+6];
const bth = _byteToHex;
return bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]];
}
module.exports = {
parse,
unparse
};
Then I used it like this:
const mc = require('mongodb').MongoClient;
const { Binary } = require('mongodb').Binary
const guidParse = require("./guid-parse.js");
const NUUID = guidString => {
return new Binary(guidParse.parse(guidString), Binary.SUBTYPE_UUID_OLD);
};
mc.connect('mongodb://localhost:27017/database').then( conn => {
const db = conn.db('database');
return db
.collection('users')
.find({
Guid: NUUID("9EC5955B-E443-456A-A520-8A87DED37EBB")
})
.toArray();
}).then( users => {
console.log(users);
});
And it returned the collection I was looking for!