How to get from n to n items in mongodb -


i'm trying create android app pulls first 1-10 documents in mongodb collection , show item in list, later when list reaches end want pull 11-20 documents in mongodb collection , goes on.

def get_all_tips(from_item, max_items):     db = client.mongotip     tips_list = db.tips.find().sort([['_id', -1]]).limit(max_items).skip(from_item)     if tips_list.count() > 0:         bson import json_util         return json_util.dumps(tips_list, default=json_util.default), response_ok     else:         return "please move along nothing see here", response_invalid      pass 

but above code not work way intended rather returns from_item max_items

example: calling get_all_tips(3,4)

it returns:

document 3, document 4, document 5, document 6

i'm expecting:

document 3, document 4

in code specifying 2 parameters.

  1. from_item: starting index of documents return
  2. max_items: number of items return

therefore calling get_all_tips(3,4) return 4 documents starting document 3 what's happening.

proposed fixes:

  1. if want return documents 3 , 4 call get_all_tips(3,2) instead, means return maximum of 2 documents starting 3.
  2. if you'd rather specify start , end indexes in function, recommend following changes:

    def get_all_tips(from_item, to_item):     if to_item < from_item:         return "to_item must greater item", bad_request     db = client.mongotip     tips_list = db.tips.find().sort([['_id', -1]]).limit(to_item - from_item).skip(from_item) 

that being said, i'd point out mongodb documentation not recommend use of skip pagination large collections. mongodb 3.2 cursor.skip


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -