记录;allowDiskUse声明方式

Aggregation aggregation = Aggregation.newAggregation(aggregationList)
                .withOptions(new AggregationOptions(true, false, null));
        AggregationResults<O> aggregationResults = MultipleMongoConfig.getMongoTemplate().aggregate(aggregation, inputType, outputType);

之前没有用过mongoDB,临时接到任务,需要将线上使用mongoDB的数据全部迁移到mysql,数据量比较大可能上亿,所以在写迁移接口的时候遇到形形色色的问题

在使用磁盘排序,使用limit时,翻页总是查询不到想要的下一页距离,解析出来的mongo 语句为

异常的语句
db.getCollection("promotion_present_log").aggregate([{ "$match": { "ployId": { "$in":["12", "434", "345", "4567"]} } },
{"$limit":20},
{"$match":{"_id":{"$gt" : {"$oid" : "5667"}}}},
{ "$sort": { "_id": 1 } }`
])

Java层代码
在这里插入图片描述

!!!正是因为这里,match没有连在一起,导致查不出数据来

正确的mongo语句

db.getCollection("promotion_present_log").aggregate([
{ "$match": { "ployId": { "$in": 
    ["12", "434", "345", "4567"]` 
    } } },
      {"$match":{"_id":{"$gt" : {"$oid" : "5ef98f9cc3abbe0c5aab6cf0"}}}},
{"$limit":20},
{ "$sort": { "_id": 1 } }
])

match条件需要在一起!!
正确的java代码,match条件需要在一起,且顺序需要 sort > skip > limit
在这里插入图片描述

总结:和磁盘分页无关,写代码写法有关,总之一句话,还是自己太菜啦!!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐