随着的MongoDB和GUID的文件的编号是什么存储GUID来轻松地检索实际的Guid有效的方法? [英] With mongodb and guids for the Id of documents what is efficient way to store the Guids to easily retrieve the actual Guid?

查看:1702
本文介绍了随着的MongoDB和GUID的文件的编号是什么存储GUID来轻松地检索实际的Guid有效的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过10gen公司提供的C#驱动程序的MongoDB版本2.06和版本(1.5)。

I'm running version 2.06 of Mongodb and version (1.5) of the C# driver supplied by 10Gen.

每个实体我有id属性设置为这样的...

Each of my entities has an Id property setup as such...

 [BsonId(IdGenerator = typeof(GuidGenerator))]
 public Guid Id { get; set; }

ID字段存储为二进制 - 3:UuidLegacy。因为它是如何当我打电话的toJSON()就可以返回该标识下面的JavaScript对象的实体存储。

The Id field is stored as Binary - 3:UuidLegacy. Because of how it is stored when I call ToJson() on an entity it returns the following javascript object for the Id.

_id : Object
 $binary: "some values here"
 $type: "03"

这显然是因为数据被存储为二进制= 3:UuidLegacy。这是有意义的。

This is obviously because the data is being stored as Binary = 3:UuidLegacy. This make sense.

我想用实际的Guid在我的JavaScript code。它是如何有效的将是MongoDB的,如果我做了我的ID属性看起来像下面?

I want to use the actual Guid in my Javascript code. How efficient would it be for MongoDB if I made my Id properties look like the following?

 [BsonId(IdGenerator = typeof(GuidGenerator)),MongoDB.Bson.Serialization.Attributes.BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }

这使得MongoDB的我的ID存储为一个字符串。但如何有效真的是这样?我猜对我的ID二进制格式更好,但我真的需要的GUID。

This makes mongodb store my Id as a string. But how efficient is this really? I'm guessing the Binary format for my Id is better, but I really need the Guid.

如何从二进制走 - 3:uuidLegacy到的GUID我需要在我的JSON?

How can I go from Binary - 3:uuidLegacy to the Guid I need in my json?

我想另一个以为会是我能只使用发送到我的$二进制值?我用的ID,才能执行查找和如我的查询字符串的一部分。

I guess another thought would be could I just use the $binary value that is sent to me? I use the Id to perform lookups and such as part of my query strings.

谢谢,

推荐答案

与GUID的工作有一些缺陷,主要涉及如何在蒙戈外壳二进制重新presentation工作,也导致历史事件在不同的驱动程序使用不同的字节顺序存储的GUID。

Working with GUIDs has a few pitfalls, mostly related to how to work with the binary representation in the mongo shell and also to historical accidents which resulted in different drivers storing GUIDs using different byte orders.

我用下面的code来说明问题:

I used the following code to illustrate the issues:

var document = new BsonDocument { { "_id", Guid.NewGuid() }, { "x", 1 } };
collection.Drop();
collection.Insert(document);
Console.WriteLine("Inserted GUID: {0}", document["_id"].AsGuid);

这时候我跑了它的输出:

which when I ran it output:

Inserted GUID: 2d25b9c6-6d30-4441-a360-47e7804c62be

当我在蒙戈外壳显示此我得到:

when I display this in the mongo shell I get:

> var doc = db.test.findOne()
> doc
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
> doc._id.hex()
c6b9252d306d4144a36047e7804c62be
>

请注意,即使为十六进制显示字节顺序不符合原来的GUID。这就是我说的是历史的偶然。所有字节都在那里,他们只是在一个不寻常为了感谢微软的实现Guid.ToByteArray()。

Notice that even when displayed as hex the byte order doesn't match the original GUID. That's the historical accident I was talking about. All the bytes are there, they're just in an unusual order thanks to Microsoft's implementation of Guid.ToByteArray().

为了帮助您在蒙戈的GUID工作外壳可以辅助函数以下文件复制到mongo.exe的存储目录:

To help you work with GUIDs in the mongo shell you could copy the following file of helper functions to the directory where mongo.exe is stored:

<一个href=\"https://github.com/rstam/mongo-csharp-driver/blob/master/uuidhelpers.js\">https://github.com/rstam/mongo-csharp-driver/blob/master/uuidhelpers.js

该文件的顶部有一些简短的文档注释,你可能会发现有用的。要在蒙戈外壳提供这些功能,你需要告诉蒙戈shell来读取该文件,因为它启动。请参见下面的示例会话:

The file has some brief documentation comments at the top that you might find helpful. To make these functions available in the mongo shell you need to tell the mongo shell to read this file as it starts up. See the following sample session:

C:\mongodb\mongodb-win32-x86_64-2.0.6\bin>mongo --shell uuidhelpers.js
MongoDB shell version: 2.0.6
connecting to: test
type "help" for help
> var doc = db.test.findOne()
> doc
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
> doc._id.hex()
c6b9252d306d4144a36047e7804c62be
> doc._id.toCSUUID()
CSUUID("2d25b9c6-6d30-4441-a360-47e7804c62be")
>

您也可以使用其他的辅助功能来查询的GUID的:

You could also use another of the helper functions to query for the GUIDs:

> db.test.find({_id : CSUUID("2d25b9c6-6d30-4441-a360-47e7804c62be")})
{ "_id" : BinData(3,"xrklLTBtQUSjYEfngExivg=="), "x" : 1 }
>

至于存储你的GUID字符串,这不是一个闻所未闻的做的事情,它绝对会让浏览和查询蒙戈数据外壳更容易,避免了所有与不同的字节顺序的问题。唯一的缺点是,它使用了更多的空间(大约两倍)。

As far as storing your GUIDs as strings, that's not an unheard of thing to do and it definitely makes viewing and querying the data in the mongo shell easier and avoids all the issues with different byte orders. The only disadvantage is that it uses more space (roughly double).

这篇关于随着的MongoDB和GUID的文件的编号是什么存储GUID来轻松地检索实际的Guid有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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