假设要为CollectionB实现自增加ID, 须要引入另一个专门计算ID的CollectionA. A中存放一条记录:{'_id':'CollectionB', 'currentIdValue':1}, 其中currentIdValue表示CollectionB的当前最大id值+1,每次往CollectionB里插入数据前,先到CollectionA查询currentIdValue 值并把这个值+1。mongodb
实现方式主要是利用MongoDB中findAndModify命令,只要每次往MongoDB里insert对象前生成ID赋值给_id就OK了,由于它的实现知足原子性,因此不存在并发问题。findAndModify自己提供了一个upsert参数,为true的话能够自动insert,但那样就不能自定义初始值了,因此不使用upsert。数据库
另,数据库“_seq”的名字如下划线开头,这样列表的时候会排在前面,容易分辨。并发
实现步骤以下:ui
> db.CollectionA.insert({'_id':'CollectionB', 'currentIdValue':0}) WriteResult({ "nInserted" : 1 }) > > ID=db.CollectionA.findAndModify( {update:{$inc:{"currentIdValue":1}}, query:{"_id":"CollectionB"}, new:true} ) { "_id" : "CollectionB", "currentIdValue" : 1 } > > db.user.save( {_id:ID.currentIdValue, uid:ID.currentIdValue, username:"zhangsan", password:"password123", info:"Test User1"} ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > > ID=db.CollectionA.findAndModify( {update:{$inc:{"currentIdValue":1}}, query:{"_id":"CollectionB"}, new:true} ) { "_id" : "CollectionB", "currentIdValue" : 2 } > > db.user.save( {_id:ID.currentIdValue, uid:ID.currentIdValue, username:"lisi", password:"test1234567", info:"Test User2"} ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > > db.user.find() { "_id" : 1, "uid" : 1, "username" : "zhangsan", "password" : "password123", "info" : "Test User1" } { "_id" : 2, "uid" : 2, "username" : "lisi", "password" : "test1234567", "info" : "Test User2" } > > db.CollectionA.find() { "_id" : "CollectionB", "currentIdValue" : 2 } > > # 执行命令增长值 > db.runCommand({findAndModify:'CollectionA',query:{_id:'CollectionB'}, update:{$inc:{'currentIdValue':1}}, new:true}); { "lastErrorObject" : { "updatedExisting" : true, "n" : 1 }, "value" : { "_id" : "CollectionB", "currentIdValue" : 3 }, "ok" : 1 } > > db.CollectionA.find() { "_id" : "CollectionB", "currentIdValue" : 3 }