如何在 mongodb shell 查询中使用 guid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5652107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use a guid in a mongodb shell query
提问by Journeyman
When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).
使用 MongoDB shell 时,如何使用 guid 数据类型(我在集合中用作 _id)。
The following format doesn't work:
以下格式不起作用:
>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});
Thanks.
谢谢。
采纳答案by Robert Stam
You have to compare the _id value against an instance of BinData (not against a string). Unfortunately the BinData constructor takes a Base64 string instead of a hex string.
您必须将 _id 值与 BinData 的实例(而不是字符串)进行比较。不幸的是 BinData 构造函数采用 Base64 字符串而不是十六进制字符串。
Your GUID value is missing two hex digits at the end, so for the purposes of this example I will assume they are "00". The following values are equivalent:
您的 GUID 值最后缺少两个十六进制数字,因此出于本示例的目的,我假设它们是“00”。以下值是等效的:
hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)
base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="
十六进制:“E3E45566-AFE4-A564-7876-AEFF6745FF00”(忽略破折号)
base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="
So your query should be:
所以你的查询应该是:
>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})
>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})
I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?
我假设二进制子类型已正确设置为 3。如果不是,则使用什么驱动程序来创建数据?
回答by Edward Weinert
You can use easily:
您可以轻松使用:
.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
回答by Todd
You could use the following js function in front of your query like so:
您可以在查询前使用以下 js 函数,如下所示:
function LUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // removes extra characters
return new UUID(hex); //creates new UUID
}
db.person.find({"_id" : LUUID("E3E45566-AFE4-A564-7876-AEFF6745FF"});
You could save the function in .js file and load it or open it before you make your query and if you copy the value from your results you should rename the function with:
您可以将函数保存在 .js 文件中并在进行查询之前加载或打开它,如果您从结果中复制值,则应将函数重命名为:
- LUUID for Legacy UUID
- JUUID for Java encoding
- NUUID for .net encoding
- CSUUID for c# encoding
- PYUUID for python encoding
- 用于旧版 UUID 的 LUUID
- 用于 Java 编码的 JUUID
- 用于 .net 编码的 NUUID
- 用于 c# 编码的 CSUUID
- 用于python编码的PYUUID