分页建议只有操作上一页、下一页的按钮。
不要使用skip,性能很差,默认:_id已经是有索引加持。
可以自己在别的字段创建索引:
//createTime创建升序索引
db.getCollection("users").createIndex( {"createTime": 1} )
//组合索引
db.getCollection("users").createIndex( {"_id": 1,"name": 1,"age": 1,"createTime": 1} )
sort时候: - 1 DESC
sort时候: 1 ASC
一些使用说明:
//skip很慢,不要这样用
db.getCollection("users").find().skip(12493793).limit(100)
//1494549744132415488
//1494549749597593600
//count很慢,不要这样用
db.getCollection("users").count()
//建议服用,性能很快
//_id:大于等于1494549744132415488的正着数100条
db.getCollection("users").find({
"_id":{
$gte: 1494549744132415488
},
})
.sort({"_id":1})
.limit(100)
//1494549744132415488(开始_id)
//1494549749597593600(结束_id)
//建议服用,性能很快
//_id:小于等于1494549749597593600的倒着数100条
db.getCollection("users").find({
"_id":{
$lte: 1494549749597593600
},
})
.sort({"_id":-1})
.limit(100)
//1494549749597593600(开始_id)
//1494549744132415488(结束_id)
//获取全表第一条
db.getCollection("users").find({}).sort({"_id":1}).limit(1)
//获取全表时间【"2022-02-18 06:40:31.977"、"2022-02-18 06:40:32.106"】第一条
db.getCollection("users").find({
"createTime":{
$gte:ISODate("2022-02-18 06:40:31.977"),
$lte:ISODate("2022-02-18 06:40:32.106")
},
})
.sort({"_id":1})
.limit(1)
//获取全表最后一条
db.getCollection("users").find({}).sort({"_id":-1}).limit(1)
//建议服用,性能很快
//_id:大于等于1494562456765849600、时间【"2022-02-18 06:40:31.977"、"2022-02-18 06:40:32.106"】在的正着数20条
db.getCollection("users").find({
"createTime":{
$gte:ISODate("2022-02-18 06:40:31.977"),
$lte:ISODate("2022-02-18 06:40:32.106")
},
//该_id获取见:获取全表时间【"2022-02-18 06:40:31.977"、"2022-02-18 06:40:32.106"】第一条
"_id":{
$gte: 1494562456765849600
},
})
.sort({"_id":1})
.limit(20)
//1494562456765849600(开始_id)
//1494562457105588224(结束_id)
//建议服用,性能很快
//_id:小于等于1494562457105588224、时间【"2022-02-18 06:40:31.977"、"2022-02-18 06:40:32.106"】在的倒着数20条
db.getCollection("users").find({
"createTime":{
$gte:ISODate("2022-02-18 06:40:31.977"),
$lte:ISODate("2022-02-18 06:40:32.106")
},
"_id":{
$lte: 1494562457105588224
},
})
.sort({"_id":-1})
.limit(20)
//1494562457105588224(开始_id)
//1494562456765849600(结束_id)
//createTime创建升序索引
db.getCollection("users").createIndex( {"createTime": 1} )
测试:
另外:
①:docker java服务设置Xms、Xmx值
FROM openjdk:8
COPY ./target/*.jar /blade-api.jar
RUN bash -c 'touch /blade-api.jar'
EXPOSE 8800
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Xmx2048m","-Xms2048m","/blade-api.jar","--spring.profiles.active=prod"]
②:mongoTemplate使用 allowDiskUse
Aggregation aggregation = newAggregation(XXX).withOptions(AggregationOptions.builder().allowDiskUse(true).build());
③:更新docker mongodb内存、swap内存
#创建时候限制
docker run -it --memory=3G --swap-memory=3G --name xxx imageId
#后期更新限制
docker update --memory 3G --memory-swap 3G containerId
附录(索引查看):
db.getCollection("sensorValue").getIndexes()
db.getCollection("sensorValueStatic").getIndexes()