Returning to the pymongo.collection.Collection.find() method, you might recall two options we have not yet used: skip and limit. These two options allow us to achieve pagination. In the sweetscomplete.domain.purchase.Purchase domain service, we add a new method, fetchPaginatedByCustKey(). In the new method, we accept a customer key as an argument in order to build a query:Â
# sweetscomplete.domain.purchase.Purchase
def fetchPaginatedByCustKey(self, custKey, skip, limit) :
query = {'customerKey' : custKey}
for doc in self.db.purchases.find(query, None, skip, limit) :
yield doc
We also accept arguments for skip and limit, which is crucial for pagination to occur properly.