Mongojs 通过 uuid _id 查找
Mongojs find by uuid _id
我正在尝试通过 _id 找到一条 "monogjs" 的记录。
我们的 _id
是 guid
(.net)。
所以我有这样的字符串 "80cd95b8-79bf-4025-933b-cabc71fbdc9f"
。
现在我尝试 "monogdb.bsonpure" 使用 "buffer" 指定 uuid 的子类型。
我尝试将它传递给 objectid()
,但它告诉我它需要是一个十六进制字符串 12/24。
我只是将它作为字符串传递,但它没有 return 任何东西
好的,这有效
将“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);
所以接受的答案对我不起作用。我在 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);
});
它返回了我正在寻找的集合!
我正在尝试通过 _id 找到一条 "monogjs" 的记录。
我们的 _id
是 guid
(.net)。
所以我有这样的字符串 "80cd95b8-79bf-4025-933b-cabc71fbdc9f"
。
现在我尝试 "monogdb.bsonpure" 使用 "buffer" 指定 uuid 的子类型。
我尝试将它传递给 objectid()
,但它告诉我它需要是一个十六进制字符串 12/24。
我只是将它作为字符串传递,但它没有 return 任何东西
好的,这有效
将“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);
所以接受的答案对我不起作用。我在 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);
});
它返回了我正在寻找的集合!