MongoDB Long/Int(长整型)的自增长主键 解决方案
今朝有幸尝芒果,发现自增长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);
});

MongoDB Long/Int(长整型)的自增长主键 解决方案的更多相关文章
- python基础知识2——基本的数据类型——整型,长整型,浮点型,字符串
磨人的小妖精们啊!终于可以归置下自己的大脑啦,在这里我要把--整型,长整型,浮点型,字符串,列表,元组,字典,集合,这几个知识点特别多的东西,统一的捯饬捯饬,不然一直脑袋里面乱乱的. 对于Python ...
- Python基础:数值(布尔型、整型、长整型、浮点型、复数)
一.概述 Python中的 数值类型(Numeric Types)共有5种:布尔型(bool).整型(int).长整型(long).浮点型(float)和复数(complex). 数值类型支持的主要操 ...
- Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)
提示:python版本2.7,windows系统 Python提供的基本数据类型:空.布尔类型.整型.长整型.浮点型.字符串.列表.元组.字典.日期 1.空(None) None,是一个特殊的值,不能 ...
- javascript没有长整型
记录一下前几天踩坑的经历. 背景:一个项目某一版之后很多用easyui的表格控件treegrid渲染的表格都显示不出来了 奇怪的地方主要有以下几点: 项目在测试环境才会这样,在本机能够正常运行,多次重 ...
- PHP长整型在32位系统中强制转化溢出
CleverCode近期遇到一个PHP项目整形转化问题,mysql有一个字段id是bigint的,里面有长整型,如id = 5147486396.可是php代码因为历史原因却部署在多台机器中,当中A机 ...
- 零基础如何学好Python 之int 数字整型类型 定义int()范围大小转换
本文主题是讲python数字类型python int整型使用方法及技巧.它是不可变数据类型中的一种,它的一些性质和字符串是一样的,注意是整型不是整形哦. Python int有多种数字类型:整型int ...
- Mac地址转换成long长整型 2
数据之间的转换可以使用 System.Convert Mac地址转换成long长整型 /// <summary> /// 解析长整形的数据使其转换为macID /// </sum ...
- Mac地址转换成long长整型
Mac地址转换成long长整型 using System;using System.Collections.Generic;using System.IO;using System.Text;usin ...
- 整型,长整型,无符号整型等 大端和小端(Big endian and Little endian)
一.大端和小端的问题 对于整型.长整型.无符号整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian ...
随机推荐
- C# 使用代码来操作 IIS
由于需要维护网站的时候,可以自动将所有的站点HTTP重定向到指定的静态页面上. 要操作 IIS 主要使用到的是“Microsoft.Web.Administration.dll”. 该类库不可以在引用 ...
- Android-Activity生命周期从onStart直接到onStop
一般应用场景中,onStart执行后都是要执行onResume,但是如果在onStart中调用了finish,会直接执行onStop.
- mysql表的创建和删除
在创建数据库表时,最好是在编辑器中写好创建表的代码,然后粘贴到命令行中,这样如果有错修改起来方便. 现在来创建一个user表: -- 打开数据库, --后面必须要有空格, 表示注释 USE mydb3 ...
- 程序编码(机器级代码+汇编代码+C代码+反汇编)
[-1]相关声明 本文总结于csapp: 了解详情,或有兴趣,建议看原版书籍: [0]程序编码 GCC调用了一系列程序,将源代码转化成可执行代码的流程如下: (1)C预处理器扩展源代码,插入所有用#i ...
- 剑指Offer26 字符串的全排列
/************************************************************************* > File Name: 26_String ...
- LeetCode 337
House Robber III The thief has found himself a new place for his thievery again. There is only one e ...
- 限额类费用报销单N+1原则
--添加通过自定义档案列表编码及档案编码查询主键 select bd_defdoc.pk_defdoc as defdoc --查询限额类费用类型主键 from bd_defdoc, bd_defdo ...
- 根据ie浏览器不同的类别选择不同的css
如果是IE浏览器则选择all-ie-only.css <!--[if IE]> <link rel="stylesheet" type="text/cs ...
- Part 6 AngularJS ng repeat directive
ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...
- DWZ (JUI) 教程 table 排序
dwz排序是后台排序,不是前台的js排序,他的流程和搜索,分页是一样的,当你点击排序的按钮时,从新发送请求刷新当前的navTable 和 dialog. <th width="60&q ...