我正在使用 pymongo。Category.city_collection.find()得到所有結果的集合。但我只想每頁顯示 5 個結果。我試過這個Category.city_collection.find().limit(5),它在第一頁上顯示 5 個結果,但是如何轉到其他頁面,如第二頁、第三頁等?我在我的 fastapi 專案中使用 mongo db。這是我的完整代碼
@router.get('/all_city')
async def all_city():
all_category = Category.city_collection.find()
category = convert_json(all_category)
return category
uj5u.com熱心網友回復:
你需要使用skip和limit
使用skip,您可以設定要跳過的檔案數。
使用limit,您可以設定頁面大小。
例子:
db.test.find({}).skip(10).limit(10)
將回傳 10 個檔案頁面大小的第 2 頁。
A skip(20).limit(10), 將回傳第 3 頁...
大多數時候,您應該對有序資料使用此方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/517996.html
標籤:Pythonpython-3.xmongodbpymongo快速API
