Mongodb NoRM and POCO
Asked Answered
F

2

2

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?

Fiora answered 8/11, 2010 at 15:33 Comment(1)
Why on earth would you use NoRM, it hasn't been updated in years and says "don't use this, use the official driver".Contraposition
B
2

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.

Bibber answered 8/11, 2010 at 15:44 Comment(0)
V
0

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.

Vulcanism answered 8/12, 2011 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.