mgo-bson.ObjectId与字符串ID

使用 mgo </ code>,看来最佳实践是将对象ID设置为 bson.ObjectId </代码>。 </ p>

这不是很方便,因为结果是ID代替了简单的 string </ code> id,而是以二进制形式存储在数据库中。 谷歌搜索似乎会产生大量问题,例如“如何从bson id中获取字符串?”,实际上,在 golang </ code>中,有 Hex()</ code>方法 ObjectId </ code>以允许您获取字符串。</ p>

在将数据从mongo导出到另一个数据库平台时,bson变得更加烦人。 处理收集的大数据并且想要将其与后台mongo DB中的某些属性合并的情况下,这意味着很多麻烦(您需要将二进制ObjectId转换为字符串才能与id结合) 在不使用bson表示形式的不同平台上)。 </ p>

我的问题是:使用 bson.ObjectId </ code>与 string </ code> id有什么好处? 如果我将我的 mongo </ code>实体存储为纯字符串ID,我会失去什么重要的东西吗? </ p>
</ div>

展开原文

原文

Using mgo, it seems that best practice is to set object ids to be bson.ObjectId.

This is not very convenient, as the result is that instead of a plain string id the id is stored as binary in the DB. Googling this seems to yield tons of questions like "how do I get a string out of the bson id?", and indeed in golang there is the Hex() method of the ObjectId to allow you to get the string.

The bson becomes even more annoying to work with when exporting data from mongo to another DB platform (this is the case when dealing with big data that is collected and you want to merge it with some properties from the back office mongo DB), this means a lot of pain (you need to transform the binary ObjectId to a string in order to join with the id in different platforms that do not use bson representation).

My question is: what are the benefits of using bson.ObjectId vs string id? Will I lose anything significant if I store my mongo entities with a plain string id?

dpyu7978
dpyu7978 谢谢(你的)信息。我猜bson的缺点是特定于用例的。但是我的问题仍然存在(使用bson.ObjectId与字符串ID的好处是什么?如果我将mongo实体存储为纯字符串ID,我会失去任何重要意义吗?)
接近 5 年之前 回复
douchenhui5569
douchenhui5569 十六进制表示形式是可移植的/兼容的。UUIDv4没什么不同-实际上,像Postgres这样的数据库会将UUID存储为字节数组,而不是字符串。
接近 5 年之前 回复
doudiecai1572
doudiecai1572 生成UUID字符串非常简单。为此目的使用ObjectId机制似乎是多余的。此外,它还具有与任何不使用bson数据类型的系统不兼容的缺点
接近 5 年之前 回复
dongpo3957
dongpo3957 您将如何生成满足唯一性要求的字符串?从外部处理ObjectID时调用ID.Hex()没有任何缺点-使用您自己的字符串ID会更大(24字节对12字节的ObjectID),并使您的实现更加脆弱。
接近 5 年之前 回复

1个回答

如注释中所述,



将ObjectId存储为十六进制字符串将使所需空间增加一倍,以防万一 提取其中一个值,您首先需要从该字符串构造一个ObjectId。</ p>

但是您有一个误解。 完全没有必要为必需的 _id </ code>字段使用ObjectId。 我经常建议不要这样做。 这就是为什么。</ p>

以简单的书本,关系和一些为简单起见而考虑的其他示例为例:</ p>

  {
_id:ObjectId(“ 56b0d36c23da2af0363abe37”),
isbn:“ 978-3453056657”,
标题:“ Neuromancer”,
作者:“ William Gibson”,
语言:“德语”
}
</ 代码> </ pre>

现在,这里的ObjectId有什么用? 其实没有。 这将是几乎没有用处的索引,因为您永远不会通过这样的人工键来搜索书籍数据库。 它没有语义价值。 对于已经已经</ em>具有全局唯一ID(即ISBN)的对象,它将是唯一ID。</ p>

因此,我们像这样简化了我们的图书文档:</ p >

  {
_id:“ 978-3453056657”,
标题:“ Neuromancer”,
作者:“ William Gibson”,
语言:“德语”
} \ n </ code> </ pre>

我们减小了文档的大小,使用了预先存在的全局唯一ID,并且没有基本未使用的索引。</ p>

回到您的基本问题,是否通过不使用ObjectIds来放松某些东西:通常,不使用ObjectId是更好的选择。 但是,如果使用它,请使用二进制形式。</ p>
</ div>

展开原文

原文

As was already mentioned in the comments, storing the ObjectId as a hex string would double the space needed for it and in case you want to extract one of its values, you'd first need to construct an ObjectId from that string.

But you have a misconception. There is absolutely no need to use an ObjectId for the mandatory _id field. Quite often, I advice against that. Here is why.

Take the simple example of a book, relations and some other considerations set aside for simplicty:

{
  _id: ObjectId("56b0d36c23da2af0363abe37"),
  isbn: "978-3453056657",
  title: "Neuromancer",
  author: "William Gibson",
  language: "German"
}

Now, what use would have the ObjectId here? Actually none. It would be an index with hardly any use, since you would never search your book databases by an artificial key like that. It holds no semantic value. It would be a unique ID for an object which already has a globally unique ID – the ISBN.

So we simplify our book document like this:

{
  _id: "978-3453056657",
  title: "Neuromancer",
  author: "William Gibson",
  language: "German"
}

We have reduced the size of the document, make use of a preexisting globally unique ID and do not have a basically unused index.

Back to your basic question wether you loose something by not using ObjectIds: Quite often, not using the ObjectId is the better choice. But if you use it, use the binary form.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐