Mongodb NoRM 和 POCO

发布于 2024-09-30 19:14:48 字数 375 浏览 1 评论 0原文

我正在 C# 中试验 Mongodb 和 NoRM。

根据我的阅读,唯一标识 Mongodb 中文档的 ObjectId 是一种“特殊”Guid(从某种意义上来说是唯一的),但其中有一些重要的部分,可以更轻松地进行排序和索引(时间戳、机器哈希) ,processId 和其中的增量)。

我想让我的对象真正保持 POCO 状态,并使用自动生成的 Guid 作为 Id,这样就没有像 ObjectId 这样的外部类型,这会阻止我迁移到另一种技术或回到经典的 Ado.net 或 Nhibernate 东西。

另一种选择是在 POCO 中使用 Id 作为字符串并使用 ObjectId.NewObjectId().ToString()

最佳实践是什么?

I am experimenting with Mongodb and NoRM in C#.

From what I have read the ObjectId that uniquely identifies a document in Mongodb is a sort of "special" Guid (in the sense that is unique) but has some significant parts in it that permit a more easy sorting and indexing (timestamp,machine hash,processId and increment in it).

I'd like to keep my objects really POCO with an autogenerated Guid as Id so with no external types like ObjectId that would prevent me to migrate to another technology or go back to classic Ado.net or Nhibernate stuff.

Another option would be to use in the POCO an Id as string and use ObjectId.NewObjectId().ToString()

What are the best practices for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-10-07 19:14:48

ObjectId 不是指南。 Guid 是 MS 为其使用的 UUID 版本提供的名称。 ObjectIds 是一种完全不同的算法。

话虽如此,您可以在 mongo 中使用任何您喜欢的 ID,并且不会产生性能损失(在 Rails 世界中,一些 ORM 提倡使用字符串)

ObjectId 在 mongo 中使用主要是为了节省大小。如果这很重要,就使用其他东西,只要意识到使用不必要的大 ID 字段最终只会浪费内存。但如果这是一件大事,那就去找Guids。

ObjectId are not guids. Guid is the name that MS gives for the version of UUID that they use. ObjectIds are a completely different algorithm.

That being said, you can use whatever you like as an ID in mongo, and it won't have performance penalties (in the rails world, a few of the ORMs advocate using strings)

ObjectId is used in mongo mostly to save size. If it is that big a deal, just use something else, just realize that using needlessly large ID fields will end up just wasting ram. But if it is a big deal, then go for Guids.

樱娆 2024-10-07 19:14:48

正如马特所说,您可以使用任何您喜欢的类型作为 _id ,只要它在其集合中是唯一的。

以 [DOC:BSON] 类型存储 UUID 是最有效的,但如果空间和速度不是问题,您也可以使用十六进制字符串。

You can use any type you like as _id as Matt said providing it's unique in it's collection.

Storing the UUID in the [DOC:BSON] type is the most efficient, but you can also use a hex string if space and speed are not an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文