在MongoDB中将Guid作为字符串存储的缺点是什么? [英] What are drawbacks of storing Guid as String in MongoDB?

查看:71
本文介绍了在MongoDB中将Guid作为字符串存储的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序在Mongo中保留了Guid字段,并且最终将其存储为BinData:

An application persists Guid field in Mongo and it ends up being stored as BinData:

"_id" : new BinData(3, "WBAc3FDBDU+Zh/cBQFPc3Q==")

在这种情况下,优点是结构紧凑,当需要对应用程序进行故障排除时,缺点就会显现出来.指南是通过URL传递的,在进入Mongo控制台时不断将其转换为BinData会很痛苦.

The advantage in this case is compactness, the disadvantage shows up when one needs to troubleshoot the application. Guids are passed via URLs, and constantly transforming them to BinData when going to Mongo console is a bit painful.

除了增加大小,将Guid存储为字符串还有什么缺点?优势之一是易于排除故障:

What are drawbacks of storing Guid as string in addition to increase in size? One advantage is ease of troubleshooting:

"_id" : "3c901cac-5b90-4a09-896c-00e4779a9199"

这是C#中持久实体的原型:

Here is a prototype of a persistent entity in C#:

class Thing
{
    [BsonIgnore]
    public Guid Id { get; set; }

    [BsonId]
    public string DontUseInAppMongoId
    {
        get { return Id.ToString(); }
        set { Id = Guid.Parse(value); }
    }
}

推荐答案

除了gregor的回答外,使用Guids当前还将阻止使用新的Aggregation Framework,因为它以二进制类型表示.无论如何,您都可以轻松地完成自己想要的事情.这将使mongodb bson库能够为您完成转换.

In addition to gregor's answer, using Guids will currently prevent the use of the new Aggregation Framework as it is represented as a binary type. Regardless, you can do what you are wanting in an easier way. This will let the mongodb bson library handle doing the conversions for you.

public class MyClass
{
  [BsonRepresentation(BsonType.String)]
  public Guid Id { get; set;}
}

这篇关于在MongoDB中将Guid作为字符串存储的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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