门户网站建设的平台,网页制作三剑客即,seo关键词排名,廊坊视频优化排名在某些特定的业务场景下#xff0c;会需要使用自增的序列来维护数据#xff0c;目前项目中因为使用MongoDB#xff0c;顾记录一下如何使用MongoDB实现自增序列。 MongoDB自增序列原理 MongoDB本身不具有自增序列的功能#xff0c;但是MongoDB的$inc操作是具有原子性的…在某些特定的业务场景下会需要使用自增的序列来维护数据目前项目中因为使用MongoDB顾记录一下如何使用MongoDB实现自增序列。 MongoDB自增序列原理 MongoDB本身不具有自增序列的功能但是MongoDB的$inc操作是具有原子性的因为操作的原子性那么就可以通过$inc序列1的值作用本次序列实现自增序列。 实现自增序列 定义自增序列IdSequence.java
Document(collection sequences)
Data
public class IdSequence {Idprivate String id;/*** 自增的序列*/private long nextId;
}定义自增序列类型枚举IdType.java
Getter
AllArgsConstructor
public enum IdType {/*** 图书*/BOOK(book);private final String type;
}定义获取自增序列辅助类IdHelper.java
Component
public class IdHelper {private final MongoTemplate mongoTemplate;public IdHelper(MongoTemplate mongoTemplate) {this.mongoTemplate mongoTemplate;}/*** 查询指定类型的自增序列** param idType 类型* return 自增序列*/public long nextId(IdType idType) {//构建查询对象Query query Query.query(Criteria.where(LambdaUtil.getFieldName(IdSequence::getId)).is(idType.getType()));//构建自增条件Update update new Update().inc(LambdaUtil.getFieldName(IdSequence::getNextId), 1L);//构建选项FindAndModifyOptions options FindAndModifyOptions.options().upsert(true).returnNew(true);//查询自增序列IdSequence idSequence mongoTemplate.findAndModify(query, update, options, IdSequence.class);//返回自增序列return Objects.requireNonNull(idSequence).getNextId();}
}定义测试类
RestController
RequestMapping(value /id)
public class IdSequenceController {Resourceprivate IdHelper idHelper;GetMapping(value /nextId)public long nextId() {return idHelper.nextId(IdType.BOOK);}}总结 使用MongoDB实现自增序列是基于MongoDB的$inc操作指令经过测试100个并发下没有发现重复id的产生但是还是建议使用MongoDB自己的Id生成策略毕竟MongoDB作为非关系型数据库如果维护一个自增序列性能上肯定没有ObjectId好而且分片情况下需要自己校验自增序列的唯一性当然在某些特殊的业务场景下必须要使用自增序列的也属于正常如果大家有更好的方案欢迎讨论。