今朝有幸尝芒果,发现自增长ID类型有多种,唯独没有Long/Int。
一思路:
1. 自建一个Collection(表,假设名为:IdentityEntity,其中字段:_id, Key, Value,其中_id你懂的,Key:Collection名,需要用Long/Int自增长主键的Collection名,value:Key字段中所存Collection的Long/Int自增长最大值),此表用于存入要用Long/Int自增长主键的Collection信息(为方便举例:XLogs)
结构自建的Collection, IdentityEntity如下:
_id Key Value
new Guid() XLogs 5
2. 每次往XLogs新增数据时,将当前最大值(5)取出来,然后再加1,我们知道自增长键是无需我们明确Insert的,当然它也一样整个过程都是由Mongo自身框架内部完成。
3. 既然是内部完成,我们就得实现一小部分代码,让Mongo框架来调用
二实现:
1. Mongo框架中的接口:IIdGenerator
#region 程序集 MongoDB.Bson.dll, v1.9.2.235
// E:\Projects\DLL\MongoDB.Bson.dll
#endregion using System; namespace MongoDB.Bson.Serialization
{
// 摘要:
// An interface implemented by Id generators.
public interface IIdGenerator
{
// 摘要:
// Generates an Id for a document.
//
// 参数:
// container:
// The container of the document (will be a MongoCollection when called from
// the C# driver).
//
// document:
// The document.
//
// 返回结果:
// An Id.
object GenerateId(object container, object document);
//
// 摘要:
// Tests whether an Id is empty.
//
// 参数:
// id:
// The Id.
//
// 返回结果:
// True if the Id is empty.
bool IsEmpty(object id);
}
}
2. 实现接口,代码:
public class LongIdGenerator<TDocument, TKey> : IIdGenerator where TDocument : class
{
private static LongIdGenerator<TDocument, TKey> _instance = new LongIdGenerator<TDocument, TKey>();
public static LongIdGenerator<TDocument, TKey> Instance { get { return _instance; } } public object GenerateId(object container, object document)
{
TKey id = default(TKey);
var collection = container as MongoCollection<TDocument>;
if (null != collection)
{
var mongoDB = collection.Database;
var idColl = mongoDB.GetCollection<IdentityEntity<TKey>>("IdentityEntity");
var keyName = document.GetType().Name;
id = RealGenerateId(idColl, keyName) ;
}
return id;
} private TKey RealGenerateId(MongoCollection<IdentityEntity<TKey>> idColl, string keyName)
{
TKey id;
var idQuery = new QueryDocument("Key", BsonValue.Create(keyName));
var idBuilder = new UpdateBuilder();
idBuilder.Inc("Value", ); var args = new FindAndModifyArgs();
args.Query = idQuery;
args.Update = idBuilder;
args.VersionReturned = FindAndModifyDocumentVersion.Modified;
args.Upsert = true; var result = idColl.FindAndModify(args);
if (!string.IsNullOrEmpty(result.ErrorMessage))
{
throw new Exception(result.ErrorMessage);
}
id = result.GetModifiedDocumentAs<IdentityEntity<TKey>>().Value;
return id;
} public bool IsEmpty(object id)
{
if (null == id)
{
return false;
}
return true;
}
}
2.2. 从上代码看我们知道,先要了解对Mongodb Collection的增,改,查等基本操作,然后理解“思路”中所提内容。注意Collection IdentityEntity在代码中已写死,当他不存在时第一次运行会自动新增此Collection。
三应用
到此已完成代码实现。即然实现,那么开始谈应用:
方式1.
public class XLogs : BaseLog, IEntity<long>
{
//应用:在long自增长键上加此特性
[BsonId(IdGenerator = typeof(LongIdGenerator<XLogs>))]
public long Id { get; set; } public byte Status { get; set; } public string CreatedBy { get; set; } public System.DateTime CreatedDate { get; set; } public string Remark { get; set; } public decimal Amount { get; set; }
}
方式2. 注册的方式,在数据进Collection XLogs之前,就要运行它
BsonClassMap.RegisterClassMap<XLogs>(rc =>
{
rc.AutoMap();
rc.SetIdMember(rc.GetMemberMap(c => c.Id));
rc.IdMemberMap.SetIdGenerator(LongIdGenerator<XLogs, long>.Instance);
});