vas3k vas3k - 1 year ago 81
Python Question

How to get sorted results in MongoKit?

How can I perform query like: db.articles.find().sort({created_at: -1}).limit(5); in MongoKit driver?

Perhaps I'm blind, but I can't find it in manual. I want to retrieve 5 last products ordered by 'created_at'. MongoKit offers to sort result list (it's more then 2000 items) and slice it. It's unaccepable, I want to sort() and limit() on database level, not python :(

Answer Source
db.articles.find().sort({_id: -1}).limit(10);

That should work a charm. The "_id" is the database ID which basically like a primary key in the old SQL days.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download