从Mongo获取BinData UUID作为字符串 [英] Get BinData UUID from Mongo as string

查看:269
本文介绍了从Mongo获取BinData UUID作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Mongo中存储了一些ID作为UUID(用于处理).他们这样返回:

I currently have some ids stored in Mongo as UUIDs (necessary for processing). They get returned like this:

"_id" : new BinData(3, "JliB6gIMRuSphAD2KmhzgQ==")

将这个值转换为字符串进行调试的简单方法是什么?

What would be an easy way to turn this value into a string for debugging?

请明确说明-应用程序可以很好地处理数据.我只需要一种从Mongo快速获取实际UUID的方法.

Just to be clear - the application can handle the data fine. I just need a way to get the actual UUID from Mongo quickly.

推荐答案

您的问题的答案比您预期的要复杂得多!复杂的主要原因是,由于历史原因(不幸的是),不同的驱动程序已使用不同的字节顺序将UUID写入数据库.您没有提到要使用哪个驱动程序,但我将以C#驱动程序为例.

The answer to your question is more complicated that you would expect! The main reason it's complicated is that for historical reasons (unfortunately) different drivers have written UUIDs to the database using different byte orders. You don't mention which driver you are using, but I'll use the C# driver as an example.

假设我使用以下代码插入文档:

Suppose I use the following code to insert a document:

var guid = new Guid("00112233-4455-6677-8899-aabbccddeeff");
collection.Insert(new BsonDocument {
    { "_id", guid },
    { "x", 1 }
});

如果我随后使用Mongo shell检查文档,则它看起来像这样:

If I then examine the document using the Mongo shell, it looks like this:

> db.test.findOne()
{ "_id" : BinData(3,"MyIRAFVEd2aImaq7zN3u/w=="), "x" : 1 }
>

Mongo shell具有一个称为hex的内置函数,您可以使用该函数将二进制值显示为十六进制字符串:

The Mongo shell has a built-in function called hex that you can use to display the binary value as a hex string:

> var doc = db.test.findOne()
> doc._id.hex()
33221100554477668899aabbccddeeff
>

仔细查看:十六进制字符串的字节顺序与C#程序中使用的原始UUID值不匹配.这是因为C#驱动程序使用了Microsoft的Guid类的ToByteArray方法返回的字节顺序(可悲的是,它以奇怪的顺序返回了字节,这一事实在很多个月以来都没有发现).其他驱动程序具有自己的特质.

Look carefully: the byte order of the hex string doesn't match the original UUID value used in the C# program. That's because the C# driver uses the byte order returned by Microsoft's ToByteArray method of the Guid class (which sadly returns the bytes in a bizarre order, which fact was not discovered for many months). Other drivers have their own idiosyncracies.

为解决这个问题,我们提供了一些用Javascript编写的辅助函数,可以将其加载到Mongo shell中.它们在此文件中定义:

To help out with this we have some helper functions written in Javascript that can be loaded into the Mongo shell. They are defined in this file:

https://github.com/mongodb/mongo- csharp-driver/blob/master/uuidhelpers.js

可以通过在命令行中提供文件名(以及--shell参数)来告知Mongo Shell在启动文件时对其进行处理.加载此文件后,我们可以访问许多帮助器函数来创建和显示作为UUID的BinData值.例如:

The Mongo shell can be told to process a file as it starts up by providing the name of the file on the command line (along with the --shell argument). Having loaded this file we have access to a number of helper functions to create and display BinData values that are UUIDs. For example:

C:\mongodb\mongodb-win32-x86_64-2.0.1\bin>mongo --shell uuidhelpers.js
MongoDB shell version: 2.0.1
connecting to: test
type "help" for help
> var doc = db.test.findOne()
> doc._id.toCSUUID()
CSUUID("00112233-4455-6677-8899-aabbccddeeff")
> db.test.find({_id : CSUUID("00112233-4455-6677-8899-aabbccddeeff")})
{ "_id" : BinData(3,"MyIRAFVEd2aImaq7zN3u/w=="), "x" : 1 }
>

在此示例中,toCSUUID函数用于将BinData值显示为CSUUID,而CSUUID函数用于使用C#驱动程序的字节顺序约定为UUID创建BinData值,以便我们可以在UUID上进行查询.其他驱动程序也具有类似的功能(toJUUID,toPYUUID,JUUID,PYUUID).

In this example the toCSUUID function is used to display a BinData value as a CSUUID and the CSUUID function is used to create a BinData value for a UUID using the C# driver's byte ordering conventions so that we can query on a UUID. There are similar functions for the other drivers (toJUUID, toPYUUID, JUUID, PYUUID).

将来的某一天,所有驱动程序都将使用具有标准字节顺序的新二进制子类型4进行标准化.同时,您必须使用与正在使用的任何驱动程序匹配的适当的辅助函数.

Some day in the future all drivers will standardize on a new binary subtype 4 with a standard byte order. In the meantime you have to use the appropriate helper function that matches whatever driver you are using.

这篇关于从Mongo获取BinData UUID作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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