Mongo Shell 将字符串转换为 nuuid 并进行比较
Mongo Shell convert string to nuuid and compare
我想做以下事情:
db
.getCollection('COLLECTION')
.find({ $where : function() { return NUUID(this.Col1) != this.Col2 }})
其中 Col1 包含 NUUID 字符串,Col2 包含 NUUID。
我想要 Col1(字符串)的 NUUID 值不等于 Col2 的行。
我收到错误:ReferenceError: NUUID is not defined
使用https://github.com/mongodb/mongo-csharp-driver/blob/master/uuidhelpers.js,解决方案是:
db
.getCollection('TestColl')
.find({$where: function(){
function CSUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
var b = hex.substr(10, 2) + hex.substr(8, 2);
var c = hex.substr(14, 2) + hex.substr(12, 2);
var d = hex.substr(16, 16);
hex = a + b + c + d;
var base64 = HexToBase64(hex);
return new BinData(3, base64);
}
function HexToBase64(hex) {
var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64 = "";
var group;
for (var i = 0; i < 30; i += 6) {
group = parseInt(hex.substr(i, 6), 16);
base64 += base64Digits[(group >> 18) & 0x3f];
base64 += base64Digits[(group >> 12) & 0x3f];
base64 += base64Digits[(group >> 6) & 0x3f];
base64 += base64Digits[group & 0x3f];
}
group = parseInt(hex.substr(30, 2), 16);
base64 += base64Digits[(group >> 2) & 0x3f];
base64 += base64Digits[(group << 4) & 0x3f];
base64 += "==";
return base64;
}
return CSUUID(this['Col1']).toString() != this['Col2'].toString()
}
})
我想做以下事情:
db .getCollection('COLLECTION') .find({ $where : function() { return NUUID(this.Col1) != this.Col2 }})
其中 Col1 包含 NUUID 字符串,Col2 包含 NUUID。
我想要 Col1(字符串)的 NUUID 值不等于 Col2 的行。
我收到错误:ReferenceError: NUUID is not defined
使用https://github.com/mongodb/mongo-csharp-driver/blob/master/uuidhelpers.js,解决方案是:
db
.getCollection('TestColl')
.find({$where: function(){
function CSUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
var b = hex.substr(10, 2) + hex.substr(8, 2);
var c = hex.substr(14, 2) + hex.substr(12, 2);
var d = hex.substr(16, 16);
hex = a + b + c + d;
var base64 = HexToBase64(hex);
return new BinData(3, base64);
}
function HexToBase64(hex) {
var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64 = "";
var group;
for (var i = 0; i < 30; i += 6) {
group = parseInt(hex.substr(i, 6), 16);
base64 += base64Digits[(group >> 18) & 0x3f];
base64 += base64Digits[(group >> 12) & 0x3f];
base64 += base64Digits[(group >> 6) & 0x3f];
base64 += base64Digits[group & 0x3f];
}
group = parseInt(hex.substr(30, 2), 16);
base64 += base64Digits[(group >> 2) & 0x3f];
base64 += base64Digits[(group << 4) & 0x3f];
base64 += "==";
return base64;
}
return CSUUID(this['Col1']).toString() != this['Col2'].toString()
}
})