I have collection of cities like this
{ "name": "something","population":2121}
there are thousands of documents like this in one collection
now, I have created index like this
$coll->ensureIndex(array("name" => 1, "population" => -1),
array("background" => true));
now I want to query like this
$cities = $coll->find(array("name" => array('$regex' => "^$name")))
->limit(30)
->sort(array("name" => 1, "population" => -1));
But this returns cities in ascending order of population. But I want result as descending order of population i.e. highest population first.
Any idea???
EDIT: I have created individual indexes on name and population. Following is output of db.city_info.getIndexes() and db.city_info.find({ "name": { "$regex": "^Ban" } }).sort({ "population": -1 }).explain(); respectively
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "city_database.city_info",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"ns" : "city_database.city_info",
"background" : 1,
"name" : "ascii_1"
},
{
"v" : 1,
"key" : {
"population" : 1
},
"ns" : "city_database.city_info",
"background" : 1,
"name" : "population_1"
}
]
and
{
"cursor" : "BtreeCursor ascii_1 multi",
"nscanned" : 70739,
"nscannedObjects" : 70738,
"n" : 70738,
"scanAndOrder" : true,
"millis" : 17461,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"name" : [
[
"Ban",
"Bao"
],
[
/^Ban/,
/^Ban/
]
]
}
}
Just look at time taken by query :-(
nameand queryingasciiname?