Mongojs通过uuid _id找到

时间:2015-01-06 07:20:11

标签: node.js mongodb mongojs

我正在尝试使用" monogjs"找到一条记录。通过_id。

我们的_idguid(。net)。

所以我有一个像"80cd95b8-79bf-4025-933b-cabc71fbdc9f"这样的字符串。

现在我试过" monogdb.bsonpure"使用"缓冲区"指定uuid的子类型。

我尝试将其传递给objectid()但是它告诉我它必须是十六进制字符串12/24。

我只是将它作为字符串传递但是它只是不返回任何内容

2 个答案:

答案 0 :(得分:0)

好的,这是有效的

采取" 80cd95b8-79bf-4025-933b-cabc71fbdc9f"删除" - "

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);

答案 1 :(得分:0)

因此,已接受的答案对我不起作用。我在this github issue中找到了一段代码,以将.net guid解析为缓冲区:

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
};

然后我像这样使用它:

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);
});

它返回了我想要的收藏!