0

I am trying to find one record with "monogjs" by _id.

Our _id is a guid (.net).

So I have something like this "80cd95b8-79bf-4025-933b-cabc71fbdc9f" as a string.

Now I tried "monogdb.bsonpure" with a "buffer" specifying the subtype of uuid.

I tried passing it to objectid() but then it tells me it need to be a hex string 12/24.

I just passed it as string but then it just does not return anything

Disposer
  • 6,201
  • 4
  • 31
  • 38
Pintac
  • 1,565
  • 3
  • 21
  • 38

2 Answers2

0

Ok this works

Take "80cd95b8-79bf-4025-933b-cabc71fbdc9f" remove "-"

var Binary = require('mongodb').Binary;

var uuid = require('node-uuid');

var base64data = new Buffer(uuid.parse(param), 'binary').toString('base64');

var bin = new Buffer(base64data, 'base64');

var id = new Binary(bin, Binary.SUBTYPE_UUID_OLD);
Pintac
  • 1,565
  • 3
  • 21
  • 38
0

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!

CovertIII
  • 1,053
  • 1
  • 11
  • 18