mongodb 的分页查询:
public List<NoticeSentRecordDto> select(NoticeSelectInfo noticeSelectInfo){ Query query = new Query().with(new Sort(Sort.Direction.DESC, "createTime")); if (noticeSelectInfo.getCurrPage() != null && noticeSelectInfo.getPageSize() != null) { query.skip((noticeSelectInfo.getCurrPage()-1)*noticeSelectInfo.getPageSize()) .limit(noticeSelectInfo.getPageSize()); } query.addCriteria(Criteria.where("isDeleted").is(0)); if(!StringUtils.isEmpty(noticeSelectInfo.getType())){ query.addCriteria(Criteria.where("type").is(noticeSelectInfo.getType())); } return template.find(query, NoticeSentRecordDto.class); }
查询个数:
template.count(query,NoticeSentRecordDto.class);
根据id修改一条记录,并返回:
public NoticeSentRecordDto selectOne(NoticeSelectInfo noticeSelectInfo){ Query query = new Query(); query.addCriteria(Criteria.where("_id").is(noticeSelectInfo.getId())); Update update = new Update().set("isRead", true); template.updateFirst(query, update, NoticeSentRecordDto.class); return template.findOne(query, NoticeSentRecordDto.class); }
设置mongobd 表保留的文档数:
MongoDB有两种集合能实现比较类似的功能。
一种是TTL(Time To Live)集合,mongod会自动删除过期的文档。
http://docs.mongodb.org/manual/tutorial/expire-data/
另一种是capped集合,该集合是一个固定大小的集合,当该集合存满数据时,mongod会自动通过覆盖最旧的文档来存放新的文档。
http://docs.mongodb.org/manual/core/capped-collections/
创建固定集合coll_testcapped,大小限制为1024个字节,文档数量限制为100。
db.createCollection("coll_testcapped2",{capped:true,size:1024,max:100});
https://www.cnblogs.com/xxcn/p/10183104.html