Mongodb NoRM and POCO

我怕爱的太早我们不能终老 提交于 2019-12-10 11:27:52

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4125275/mongodb-norm-and-poco

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!