using MongoDB.Bson; using System; using System.Collections.Generic; using System.Text; namespace CZFW.MDB.Util { //自增字段实现 public class AutoInc { public static int GetNext(string sid, string pageName, string tplName) { IMongoRepository mongoRepository = new MongoRepository($"{tplName}_{Constants.AUTO_INC_TABLE_NAME}"); var filter = new BsonDocument(Constants.AUTO_INC_FIELD, $"{sid}_{pageName}"); var update = BsonDocument.Parse($"{{$inc:{{{Constants.AUTO_INC_VALUE_FIELD}:1}}}}"); var res = mongoRepository.CurrentTable.FindOneAndUpdate(filter, update,new MongoDB.Driver.FindOneAndUpdateOptions { IsUpsert=true, ReturnDocument=MongoDB.Driver.ReturnDocument.After}); return res[Constants.AUTO_INC_VALUE_FIELD].AsInt32; } } }