nam
nam

Reputation: 3632

Get specific range of results from mongoDB with python

I'm using mongoDB with python to create a experiment blog website. For example I have 100 blog posts and I want to divide into 5 parts. How do I write the query to retrieve the first 20 blog posts, and then the second 20 blog posts (from post 21 to 40...) The obvious way is to retrieve all the posts into memory and use for example:

firstposts = posts[0, 20]

But I wonder if I can retrieve directly 20 first posts instead of

db.posts.find()

Thanks,

Upvotes: 0

Views: 779

Answers (1)

nutlike
nutlike

Reputation: 4975

Have a look at the skip(…) and limit(…) methods here: http://api.mongodb.org/python/current/api/pymongo/cursor.html.

db.posts.find().skip(0).limit(20);
…
db.posts.find().skip(20).limit(20);
…

Upvotes: 4

Related Questions