views:

83

answers:

1

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 :(

+1  A: 

Even Megan Fox could solve this one.

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.

How are you finding Mongo? I have been using it for a few projects and really love this DB.

John Ballinger
Ok, this mean I was stupid. conn.test.cart.find().sort("_id", 1) - (without braces) really works. Sorry for my inattention. And how to sort with many fields? .sort().sort() ?
vas3k
sort({_id:-1, title:-1})So just add a comma between each argument. This {} bracket object notation is pretty universal, json, python, actionscipt, javascript.
John Ballinger