简体   繁体   English

Spring数据mongodb查询skip()无法正常工作。

[英]Spring data mongodb query skip() works not how it should.

I discovered that skip() in a query using mongoOperations acts funny. 我发现使用mongoOperations的查询中的skip()表现有趣。

I have this code: 我有以下代码:

if (question.getCategory() == "" && question.getDifficulty() == 0 && question.getNumberOfCorrectAnswers() == 0) 
        return mongoOperations.find(new Query(Criteria.where("question").regex(questionPattern, "i"))
        .limit(getLimit()).skip(getSkip()), Question.class);

I use it in my search method. 我在搜索方法中使用它。 I want to do a pagination. 我想做一个分页。 For example limit the results to 10 and skip the first 5. 例如,将结果限制为10,然后跳过前5个。

limit() works just fine, but skip() when it gets the value of 1 displays the 1st document in the collection. limit()效果很好,但是当skip()的值为1时,它将显示集合中的第一个文档。 limit() is on 0 by default as to not shorten the range. limit()默认为0,以不缩短范围。

I checked in mongodb shell and skip(1) is viewing documents starting from the second one. 我检查了mongodb shell,skip(1)从第二个开始查看文档。

Anybody got an idea how to fix this? 有人知道如何解决此问题吗?

Can you try this instead: 您可以尝试以下方法吗:

Criteria criteria = Criteria.where("question").regex(questionPattern, "i");
Query query = Query.query(criteria);
query.limit(getLimit());
query.skip(getSkip());
return mongoOperations.find(query, Question.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM