thkang
thkang

Reputation: 11543

pymongo - querying most recent N items

What is the 'proper way' to retrieve most recent N items from the database?

from tutorial and mongodb documentations it seems (besides using range queries)

db.collection.find(skip = 0, limit=N, sort=[("_id", -1)])

is it right?

Upvotes: 3

Views: 3049

Answers (1)

Randall Hunt
Randall Hunt

Reputation: 12572

Your syntax doesn't really seem quite right there.

db.collection.find({}).sort("_id", -1).limit(N)

Should do what you would expect.

Python and pymongo support simple chaining.


NOTE
Sorting on _id does not necessarily yield the "most recent" item.

Upvotes: 5

Related Questions