C#-生成“身份"像ID与NoSQL(MongoDB)一样? [英] C# - Generate "identity" like Id's with NoSQL (MongoDB)?
问题描述
我一直在将MongoDB与C#一起使用,并且一直在使用ObjectId,然后是我的实体ID的更高版本的GUID.我讨厌看这些Id,我认为这很不直观...我真的很希望能够使用整数或long,例如关系DBs Identity列.但是我很难找到一种方法来做到这一点.如果我使用collection max + 1,那将导致比赛条件.我已经读过有关使用Hi-Lo Generator算法的信息,但是它是如何工作的呢?如果我有10个运行相同代码的应用服务器怎么办?这是否意味着每个集合我必须具有10个hi身份范围?
I've been using MongoDB with C#, and have been using ObjectIds, then later GUIDs for my Entity Ids. I hate looking at these Id's, I think it's counter intuitive... I'd really like to be able to use integers or longs, like relational DBs Identity column. But I'm having a hard time finding a way to do it. If I use collection max + 1, that will lead to race conditions. I've read about using a Hi-Lo Generator algorithm, but how does that work? What if I have 10 app servers running the same code? Does that mean I have to have 10 hi lo identity ranges per collection?
如果有人可以指出我要使用C#算法进行共享,那就太好了!顺便说一句,我已经看过RNGCryptoServiceProvider,但这不能保证是唯一的,并且它不是顺序的int/long身份.那时候我宁愿使用Hi-Lo ...
If someone can point me to a C# algorithm to share that would be great! I've seen RNGCryptoServiceProvider by the way, but that's not guaranteed to be unique, and it's not a sequential int/long Identity. I'd rather use Hi-Lo at that point...
谢谢, 蒂姆
推荐答案
您可以编写自己的ID生成器并使用它. 此处,您可以找到方法.但是ObjectId
被设计为在所有分片/副本集中都是唯一的.因此,每个分片可以独立于其他分片生成唯一的ID.在分布式数据库中使用int id会遇到很多问题.
You can write your own id generator and use it. Here you can find how. But ObjectId
was designed to be unique across all shards/replica sets. So each shard can generate unique id independently from others. With int id in distributed database you will have many problems.
这篇关于C#-生成“身份"像ID与NoSQL(MongoDB)一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!