开发者

How to get sorted results in MongoKit?

开发者 https://www.devze.com 2023-01-07 10:36 出处:网络
How can I perform query like: db.articles.find().sort({created_at: -1}).limit(5); in MongoKit driver?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消