跳过和限制Mongo聚合的分页 [英] Skip and Limit for pagination for a Mongo aggregate
问题描述
我正在使用flask-paginate(仅用于参考)在烧瓶(Python框架)中进行分页
I am working on pagination in flask(Python framework) using flask-paginate (just for ref)
我能够仅通过find
查询实现分页,如下所示:
I am able to achieve pagination for just a find
query as below:
from flask_paginate import Pagination
from flask_paginate import get_page_args
def starting_with_letter(letter):
page, per_page, offset = get_page_args()
collection_name=letter.lower()+'_collection'
words=db[collection_name]
data_db=words.find()
data=data_db.limit(per_page).skip(offset) '''Here I have achieved the limit and skip'''
pagination = Pagination(page=page, total=data.count(),per_page=per_page,offset=offset,record_name='words')
return render_template('startingwords.html',data=data,pagination=pagination)
但是我无法对此处的汇总执行相同的操作
But I am not able to do the same for the aggregate here:
def test():
page, per_page, offset = get_page_args()
cursor_list=[] '''appending each cursor in iteration of for loop '''
collections=db.collection_names()
for collection in collections:
cursor_objects = db[collection].aggregate([
{
"$match": {
"$expr": {"$eq": [{"$strLenCP": "$word"}, 6]}
}
},
{"$skip": offset},
{"$limit": per_page}
])
for cursor in cursor_objects:
cursor_list.append(cursor)
pagination = Pagination(page=page, total=len(cursor_list),per_page=per_page,offset=offset,record_name='words')
return render_template('lettersearch.html',data=cursor_list,pagination=pagination)
结果显示为:
所有39
结果都显示在一页上
Here all the 39
results are shown at single page
点击page 2
时显示:
注意:默认情况下,flask-paginate最初将per_page
设置为10
,将offset
设置为0
Note: By default flask-paginate sets initially per_page
as 10
and offset
as 0
在引用了我尝试过的许多链接之后:
after referring many links i have tried:
在match
上方放置skip
和limit
,这是错误的
placing skip
and limit
above match
which is wrong any way
还了解到limit
后面总是skip
我对此一无所知,不胜感激
I am stuck with this, Any help is appreciated
推荐答案
您的问题与skip()
和limit()
无关;一切正常.问题在于您的整体逻辑;您要在第一个循环中迭代所有39个集合,然后将聚合的每个结果附加到cursor_list
.
Your issue is not with the skip()
and limit()
; that is working fine. The issue is with your overall logic; you are iterating all 39 collections in the first loop and then appending each result of the aggregation to cursor_list
.
我无法弄清楚您要做什么的逻辑,因为第一个示例正在查看单词集合,第二个示例是在所有集合中查找单词字段;话虽如此,您可能可以简化方法,例如:
I can't figure out the logic of what you are trying to do, as the first example is looking in a words collection and second is looking in all collections for a word field; with that said, you can likely simplify your approach to something like:
offset = 0
per_page = 10
collections = db.list_collection_names()
#
# Add some logic on the collections array to filter what is needed
#
print(collections[offset:offset+per_page])
编辑以反映评论.完成此功能的完整示例.无需聚合查询-这会增加复杂性.
EDIT to reflect comments. Full worked example of a function to perform this. No need for an aggregation query - this adds complexity.
from pymongo import MongoClient
from random import randint
db = MongoClient()['testdatabase1']
# Set up some data
for i in range(39):
coll_name = f'collection{i}'
db[coll_name].delete_many({}) # Be careful; testing only; this deletes your data
for k in range (randint(0, 2)):
db[coll_name].insert_one({'word': '123456'})
# Main function
def test(offset, per_page, word_to_find):
found = []
collections = db.list_collection_names()
for collection in sorted(collections):
if db[collection].find_one({word_to_find: { '$exists': True}}) is not None:
found.append(collection)
print(found[offset:offset+per_page])
test(offset=0, per_page=10, word_to_find='word')
这篇关于跳过和限制Mongo聚合的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!