AutoInc.cs 884 B

123456789101112131415161718192021
  1. using MongoDB.Bson;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace CZFW.MDB.Util
  6. {
  7. //自增字段实现
  8. public class AutoInc
  9. {
  10. public static int GetNext(string sid, string pageName, string tplName)
  11. {
  12. IMongoRepository mongoRepository = new MongoRepository($"{tplName}_{Constants.AUTO_INC_TABLE_NAME}");
  13. var filter = new BsonDocument(Constants.AUTO_INC_FIELD, $"{sid}_{pageName}");
  14. var update = BsonDocument.Parse($"{{$inc:{{{Constants.AUTO_INC_VALUE_FIELD}:1}}}}");
  15. var res = mongoRepository.CurrentTable.FindOneAndUpdate<BsonDocument>(filter, update,new MongoDB.Driver.FindOneAndUpdateOptions<BsonDocument, BsonDocument> { IsUpsert=true, ReturnDocument=MongoDB.Driver.ReturnDocument.After});
  16. return res[Constants.AUTO_INC_VALUE_FIELD].AsInt32;
  17. }
  18. }
  19. }