我如何在mongodb shell查询中使用GUID [英] How do I use a guid in a mongodb shell query

查看:298
本文介绍了我如何在mongodb shell查询中使用GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用MongoDB shell时,如何使用guid数据类型(在我的集合中用作_id).

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

以下格式不起作用:

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

谢谢.

推荐答案

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

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.

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

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:

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

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

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

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

所以您的查询应该是:

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

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

我假设二进制子类型正确设置为3.如果不是,则使用什么驱动程序来创建数据?

I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?

这篇关于我如何在mongodb shell查询中使用GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆