find() and sort() syntax

6,581 views
Skip to first unread message

Marcelo Barbudas

unread,
Feb 19, 2011, 12:26:41 PM2/19/11
to mongoo...@googlegroups.com
Hi.

How does .sort() work in mongoose 1.x? The only references I could find in the tests was using .where().sort(), but where() supports only one key value pair.

Doing something like myModel.where('one', 'two').where('three', 'four').where('four', 'five').sort('something', '-1 (has to guess what -1 was)').find() seems really bad.
Tried myModel.where({one: 'two', three: 'four'}).sort().find() and it happily ignored the conditions.

The mongoosejs.com docs are really confusing, they barely touch query syntax.

-M.

mkoistinen

unread,
Feb 19, 2011, 3:54:24 PM2/19/11
to mongoo...@googlegroups.com
Hi Marcelo,

I'm pretty new too, but let me help where I can.

You can sort in straight find() queries too, like this:

Users.find({ some-criteria }, { lastname: 1, firstname: 1, password: 0 }, function(err, users) {... }).

This will return the collection from Users that matches your criteria in the order lastname (ascending), then firstname (ascending) and it will omit the password field from the results.  Perhaps this would be more intuitive then using where() for you?

Marcelo Barbudas

unread,
Feb 19, 2011, 4:17:00 PM2/19/11
to mongoo...@googlegroups.com
Hi Martin,

Thanks for taking the time to answer.

myModel.find(conditions, [], { sort: ['updated_at', 1], limit: 3 };

limit works, sort doesn't. I'm trying to sort DESC.

-
M.

Marcelo Barbudas

unread,
Feb 19, 2011, 4:17:58 PM2/19/11
to mongoo...@googlegroups.com
Doesn't work with -1 in sort either.

mkoistinen

unread,
Feb 19, 2011, 4:35:46 PM2/19/11
to mongoo...@googlegroups.com
Assuming 'updated_at' is a field in your document.

Have you tried:

myModel.find(conditions, [], { updated_at: -1, limit: 3 }, function());

?

mkoistinen

unread,
Feb 19, 2011, 4:41:34 PM2/19/11
to mongoo...@googlegroups.com
Actually, its not working for me either....

Marcelo Barbudas

unread,
Feb 20, 2011, 9:24:38 AM2/20/11
to mongoo...@googlegroups.com
Anyone? Still haven't figured it out.

-m.

Zoltan Kalmar

unread,
Feb 20, 2011, 9:35:54 AM2/20/11
to mongoo...@googlegroups.com, Marcelo Barbudas
Syntax of sorting:

Users.find({ some-criteria }, array_of_field_names)
.sort('sort_field_name', -1) // DESC order
.execFind( function(err, users) {... }); // since mongoose 1.0.14

On Sun, Feb 20, 2011 at 3:24 PM, Marcelo Barbudas <nos...@gmail.com> wrote:
> Anyone? Still haven't figured it out.
> -m.

--
kalmi

mkoistinen

unread,
Feb 20, 2011, 9:46:15 AM2/20/11
to mongoo...@googlegroups.com, Marcelo Barbudas
Works perfectly for me, thanks Kalmi!

Marcelo Barbudas

unread,
Feb 20, 2011, 10:05:52 AM2/20/11
to mongoo...@googlegroups.com, Marcelo Barbudas
Thanks for the help kalmi.

easternbloc

unread,
Feb 20, 2011, 6:40:32 PM2/20/11
to Mongoose Node.JS ORM
Huzzah, thanks for this. Is the API on the website now out of date
then?

Shunsuke Watanabe

unread,
Feb 20, 2011, 11:05:29 PM2/20/11
to Mongoose Node.JS ORM
Hi, all.

Kalmi's example doesn't work for me.
I'm using Mongoose 1.0.16 on node 0.2.6

This works fine.

User.find({/* conditions */},[/* fields */],{sort:[['date',-1]], limit:
10},function(err,docs){
});

But I don't like this syntax.
It's obtrusive to read and write.

The old way is better, isn't it?

User.find({}, function(err, docs){}).sort('date',-1).limit(10);

I begin to think of abandoning to use Mongoose...

Guillermo Rauch

unread,
Feb 20, 2011, 11:59:33 PM2/20/11
to mongoo...@googlegroups.com
User.find({}, ['field']).sort([['date'], -1).limit(10).run(function(err, docs){

}).

--
Guillermo Rauch
http://devthought.com

Shunsuke Watanabe

unread,
Feb 21, 2011, 2:09:48 AM2/21/11
to Mongoose Node.JS ORM
Hi, Mr.Rauch.

Thanks for your example.

I tried run() method and got this:
TypeError: Object #<a Query> has no method 'run'

easternbloc

unread,
Feb 21, 2011, 6:27:54 AM2/21/11
to Mongoose Node.JS ORM
Ditto, only works with execFind.

Shunsuke Watanabe

unread,
Feb 22, 2011, 1:56:37 AM2/22/11
to Mongoose Node.JS ORM
Yeah, I could make Kalmi's example worked.
I mistakenly passed array to sort method.

michael.grant

unread,
Aug 26, 2012, 6:58:38 AM8/26/12
to mongoo...@googlegroups.com, craf...@gmail.com
this should work:

find({}, [], { sort: [['stamp', -1]], limit: 10 }, function( error, ecollection){

});

Sajith Kumar Arimbra

unread,
May 23, 2014, 6:07:57 AM5/23/14
to mongoo...@googlegroups.com
this works for me

Category.find({parentuid:catid}, null, {sort:{name:1}, skip:0, limit:20}, function(err, categories) {  
 callback(err, categories);
 });
Reply all
Reply to author
Forward
0 new messages