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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With