Possible to sort by multiple keys?

6,215 views
Skip to first unread message

D. Smith

unread,
Mar 9, 2011, 11:10:39 AM3/9/11
to mongodb-user
Is it possible to sort results by 2 or more keys?

In analogy to mysql, I need something like this:

SELECT * FROM FORUM_POSTS
ORDER BY
is_sticky ACS,
last_modified DESC

So basically I need to select items from forum_posts collection and
have posts marked with 'is_sticky' = true to appear first,
followed by posts ordered by last_modified time

Is it possible and how does mongo handle this? I mean - will it use 2
keys: is_sticky and last_modified or should I create a composite key?
I would rather not do a composite key unless it is really the best
option.

Thanks.

Scott Hernandez

unread,
Mar 9, 2011, 11:21:27 AM3/9/11
to mongod...@googlegroups.com
Yes, you can sort by multiple fields; it almost never a good idea to
concatenate multiple fields together to make a new one.

find().sort({field1:1, field2:0}) for example.

http://www.mongodb.org/display/DOCS/Querying#Querying-Sorting

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Scott Hernandez

unread,
Mar 9, 2011, 11:49:24 AM3/9/11
to mongod...@googlegroups.com
Sorry, 1 = ascending, -1 = descending.

The 0 below is a typo.

D. Smith

unread,
Mar 9, 2011, 12:17:22 PM3/9/11
to mongodb-user

Scott Hernandez

unread,
Mar 9, 2011, 12:48:41 PM3/9/11
to mongod...@googlegroups.com
Yes, see my previous response.

Luiz Felipe Mendes

unread,
Jan 30, 2013, 8:52:04 AM1/30/13
to mongod...@googlegroups.com
Hello,
I'm very sorry to be resuscitating this old thread, but I would like to know, what is the best index to use in queries with multiples sort fields. 

Like:

db.Collection.find({ "$and" : [ { "m" : BinData(0,"wzlbR39bEokaufRAta5/hw==")}], "lala" : true , "date" : { "$gte" :  ISODate("2013-01-22T03:00:00.000Z")} ,  "date" : { "$lte" : ISODate( "2013-01-29T20:48:44.000Z")} , "o.lol" : { "$in" : [ 0 , 2]}}).limit(10).sort({"date":1,"id":1})

ps: In this case, the results will be ordered first by "date" and then by "id", right?

Thanks

Nirmalkumar Seshachalam

unread,
May 26, 2013, 5:05:47 AM5/26/13
to mongod...@googlegroups.com
Dear Sir,

find().sort({field1:1, field2:1})

Looks does not work. For example I tried to apply on following data


The structure is like this: { "_id" : { "$oid" : "513257f68d6e7cb63d7b1c1f" }, "student_id" : 0, "type" : "homework", "score" : 78.45394447385343 }

In this I tries to sort first on student_id and then on score. This is essentially to list(table) each students record in the order of their score. So I tried the query bellow

db.grades.find().sort({student_id:1},{score:1})

It does not help me to achieve the desired result. Could you pls help?

Thanks,
Nirmalkumar Seshachalam

Sam Millman

unread,
May 26, 2013, 9:44:52 AM5/26/13
to mongod...@googlegroups.com
db.grades.find().sort({student_id:1,score:1})


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages