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() a开发者_JS百科nd limit() on database level, not python :(
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.
Correct MongoKit/PyMongo equivalent is
connection.articles.find().sort('created_at', pymongo.DESCENDING).limit(5)
More at PyMongo cursor doc.
精彩评论