如何在 MongoDB 中使用范围查询进行分页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5525304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do pagination using range queries in MongoDB?
提问by Donny V.
I'm probably missing something since I'm still learning the ins and outs of MongoDB, but I need help with paging a collection.
我可能遗漏了一些东西,因为我仍在学习 MongoDB 的来龙去脉,但我需要帮助来分页集合。
I have a collection that has a list of names.
我有一个包含名称列表的集合。
Bottom Round of Beef
Chicken Breast 6oz
Chicken Breast 8oz
Chicken Breast 8oz
Chicken Breast 8oz
Chicken Breast Random
Chicken Legs
Chicken Tenderloin
Chicken Thighs
Kosher Salt
底部圆牛
鸡胸肉 6oz 鸡胸肉
8oz
鸡胸肉 8oz
鸡胸肉 8oz
鸡胸肉 随机
鸡腿
鸡里脊肉
鸡大腿
犹太盐
I created a compound index on "ProductName,_id".
我在“ProductName,_id”上创建了一个复合索引。
I run this query:
我运行这个查询:
db.ProductGuideItem.find( { ProductName: { $gt: "Chicken Breast 8oz" } } ).sort({ProductName:1,_id:1}).limit(3);
Notice there are 3 "Chicken Breast 8oz" items.
请注意,有 3 个“鸡胸肉 8 盎司”商品。
If I run that query I get...
Chicken Breast Random
Chicken Legs
Chicken Tenderloin
如果我运行该查询,我会得到...
鸡胸肉 随机
鸡腿
鸡里脊肉
If I was paging and started from the top. The query would have missed the other 2 "Chicken Breast 8oz".
如果我正在分页并从顶部开始。查询会漏掉另外 2 个“Chicken Breast 8oz”。
So if each page can only have 3 items and I want to see page 2 then I should see..
Chicken Breast 8oz
Chicken Breast 8oz
Chicken Breast Random.
所以如果每页只能有 3 个项目,而我想看第 2 页,那么我应该看到..
鸡胸肉 8oz
鸡胸肉 8oz
鸡胸肉 随机。
But I'm not. It's going to the last Chicken Breast 8oz and starting from there instead.
但我不是。它会到最后的鸡胸肉 8 盎司,然后从那里开始。
Is there a way around this?
Also how would I do this if the list was sorted the opposite way?
有没有解决的办法?
另外,如果列表以相反的方式排序,我将如何执行此操作?
回答by Donny V.
Since the collection I was paging had duplicate values I had to create a compound index on ProductName and id.
由于我正在分页的集合有重复的值,我必须在 ProductName 和 id 上创建一个复合索引。
Create Compound Index
创建复合索引
db.ProductGuideItem.ensureIndex({ ProductName:1, _id:1});
This solved my problem.
Reference: https://groups.google.com/d/msg/mongodb-user/3EZZIRJzW_A/oYH79npKZHkJ
这解决了我的问题。
参考:https: //groups.google.com/d/msg/mongodb-user/3EZZIRJzW_A/oYH79npKZHkJ
Assuming you have these values:
假设你有这些值:
{a:1, b:1}
{a:2, b:1}
{a:2, b:2}
{a:2, b:3}
{a:3, b:1}
So you do this for the range based pagination (page size of 2):
因此,您对基于范围的分页(页面大小为 2)执行此操作:
1st Page
第一页
find().sort({a:1, b:1}).limit(2)
{a:1, b:1}
{a:2, b:1}
2nd Page
第二页
find().min({a:2, b:1}).sort({a:1, b:1}).skip(1).limit(2)
{a:2, b:2}
{a:2, b:3}
3rd Page
第三页
find().min({a:2, b:3}).sort({a:1, b:1}).skip(1).limit(2)
{a:3, b:1}
Here are the docs for $min/max: http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers
以下是 $min/max 的文档:http: //www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers
If you don't have duplicate values in your collection, you don't need to use min & max or create a compound index. You can just use $lt & $gt.
如果您的集合中没有重复值,则不需要使用 min & max 或创建复合索引。你可以只使用 $lt & $gt。
回答by moshe beeri
You just need to call skip on the cursor
你只需要在光标上调用skip
see MongoDB documentation on cursor skip"This approach may be useful in implementing “paged” results".
请参阅有关光标跳过的MongoDB 文档“此方法可能有助于实现“分页”结果”。
This example is taken from Mongodb's example
这个例子取自Mongodb的例子
function printStudents(pageNumber, nPerPage) {
print("Page: " + pageNumber);
db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}