如何在 mongodb shell 查询中使用 guid

发布于 2024-11-01 06:01:55 字数 177 浏览 6 评论 0 原文

使用 MongoDB shell 时,如何使用 guid 数据类型(我已将其用作集合中的 _id)。

以下格式无效:

>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});

谢谢。

When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).

The following format doesn't work:

>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暖风昔人 2024-11-08 06:01:55

您可以轻松使用:

.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})

You can use easily:

.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
多像笑话 2024-11-08 06:01:55

您必须将 _id 值与 BinData 实例进行比较(而不是与字符串进行比较)。不幸的是 BinData 构造函数采用 Base64 字符串而不是十六进制字符串。

您的 GUID 值末尾缺少两个十六进制数字,因此出于本示例的目的,我将假设它们为“00”。以下值是等效的:

十六进制:“E3E45566-AFE4-A564-7876-AEFF6745FF00”(忽略破折号)

base64:“ZlXk4+SvZKV4dq7/Z0X/AA==”

所以您的查询应该是:

>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})

我假设二进制子类型已正确设置为 3。如果没有,则使用了什么驱动程序创建数据?

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.

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:

hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)

base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="

So your query should be:

>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?

茶底世界 2024-11-08 06:01:55

您可以在查询前面使用以下 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"});

您可以将函数保存在 .js 文件中并加载它或在进行查询之前打开它,如果您从结果中复制值,则应将函数重命名为:

  • LUUID(用于传统 UUID)
  • JUUID(用于 Java 编码)
  • NUUID(用于 .net 编码)
  • CSUUID(用于 c# 编码)
  • PYUUID(用于 python 编码)

You could use the following js function in front of your query like so:

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:

  • LUUID for Legacy UUID
  • JUUID for Java encoding
  • NUUID for .net encoding
  • CSUUID for c# encoding
  • PYUUID for python encoding
葬﹪忆之殇 2024-11-08 06:01:55

我知道这是一个老问题,但如果没有任何额外的需求,您可以使用这个:

find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})

I know it's an old issue, but without any additional needs you can use this one:

find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})
梦纸 2024-11-08 06:01:55

老问题的新答案,但该线程中的代码示例都不适合我。

看起来 BinData 类型 03 有多个子类型,具体取决于语言。检查 mongo-java-driver 代码 此处

对于Java,UUID被分成两半,然后每一半被翻转。

此 python 代码可以将 UUID 转换为 BinData() 类型 03 子类型 JAVA_LEGACY 并返回 UUID:

import base64
import uuid

def mongo_bin_data_to_uuid(bin_data_base64):
    raw_bytes = base64.b64decode(bin_data_base64)
    flipped_bytes = raw_bytes[7::-1] + raw_bytes[-1:7:-1]
    return str(uuid.UUID(bytes=flipped_bytes))

def uuid_to_mongo_bin_data(uuid_string):
    raw_bytes = uuid.UUID(uuid_string).bytes
    flipped_bytes = raw_bytes[7::-1] + raw_bytes[-1:7:-1]
    return base64.b64encode(flipped_bytes).decode()

uuid_to_mongo_bin_data("E3E45566-AFE4-A564-7876-AEFF6745FF00")

所示

"ZKXkr2ZV5OMA/0Vn/652eA=="

查询的结果如下

>db.person.find({"_id", BinData(03, "ZKXkr2ZV5OMA/0Vn/652eA==")});

New answer to an old question, but none of the code examples in this thread worked for me.

It looks like BinData type 03 has multiple subtypes, depending on the language. Check the mongo-java-driver code here.

For Java, the UUID is split into two halves and then each half is flipped.

This python code can translate UUIDs to BinData() type 03 subtype JAVA_LEGACY and back to UUID:

import base64
import uuid

def mongo_bin_data_to_uuid(bin_data_base64):
    raw_bytes = base64.b64decode(bin_data_base64)
    flipped_bytes = raw_bytes[7::-1] + raw_bytes[-1:7:-1]
    return str(uuid.UUID(bytes=flipped_bytes))

def uuid_to_mongo_bin_data(uuid_string):
    raw_bytes = uuid.UUID(uuid_string).bytes
    flipped_bytes = raw_bytes[7::-1] + raw_bytes[-1:7:-1]
    return base64.b64encode(flipped_bytes).decode()

The result of

uuid_to_mongo_bin_data("E3E45566-AFE4-A564-7876-AEFF6745FF00")

is

"ZKXkr2ZV5OMA/0Vn/652eA=="

The query then looks like:

>db.person.find({"_id", BinData(03, "ZKXkr2ZV5OMA/0Vn/652eA==")});
痴情 2024-11-08 06:01:55

您可以使用 split() 和 join() 解决方法来解决此问题:

例如,如果我使用 "E3E45566-AFE4-A564-7876-AEFF6745FF" 十六进制值和 -UUID() 函数内部,它不会在 mongo 中返回 BinData,因此请尝试在传递给 UUID-代码>函数。

db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});

或者通过定义一个变量来在多行中执行此操作:

var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});

或者通过创建一个简单的函数:

function BUUID(uuid){
    var str = uuid.split("-").join('');
    return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();

You can fix this issue by using split() and join() workaround:

for instance if I use "E3E45566-AFE4-A564-7876-AEFF6745FF" hex value with - inside UUID() function, it does not return BinData in mongo so please try removing all the - before passing to UUID function.

db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});

Or by defining a variable to do it in multiple line:

var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});

or by creating a simple function:

function BUUID(uuid){
    var str = uuid.split("-").join('');
    return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文