I am developing a site in ruby on rails using mongoDB database. I am a beginner in mongoDB and I want to sort data using multiple keys. One of my collection is shown below.
dd_users
{
"_id": ObjectId("4e9bcbdb1d41c866c600004e"),
"name": "Ram",
"score":5
"created_at": ISODate("2011-10-17T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600004f"),
"name": "Robert",
"score":1
"created_at": ISODate("2011-10-12T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600004g"),
"name": "Molly Bracken",
"score":3
"created_at": ISODate("2011-10-15T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600005k"),
"name": "Ninou-Co France",
"score":5
"created_at": ISODate("2011-10-16T06: 31: 55.0Z"),
}
I want to sort the records in descending order of ‘score’ and ‘created_at’.
I used the mongomapper query as
1.DdUser.where({“name” => { “$ne” => nil}).sort([[“score”,”desc”],[“created_at”,”desc”]]). But am getting the result in the order of score only.
I am expecting the result
{
"_id": ObjectId("4e9bcbdb1d41c866c600004e"),
"name": "Ram",
"score":5
"created_at": ISODate("2011-10-17T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600005k"),
"name": "Ninou-Co France",
"score":5
"created_at": ISODate("2011-10-16T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600004g"),
"name": "Molly Bracken",
"score":3
"created_at": ISODate("2011-10-15T06: 31: 55.0Z"),
}
{
"_id": ObjectId("4e9bcbdb1d41c866c600004f"),
"name": "Robert",
"score":1
"created_at": ISODate("2011-10-12T06: 31: 55.0Z"),
}
1 Answer