Skip to content

Instantly share code, notes, and snippets.

@tanordheim
Created January 27, 2012 23:31
Show Gist options
  • Save tanordheim/1691573 to your computer and use it in GitHub Desktop.
Save tanordheim/1691573 to your computer and use it in GitHub Desktop.
MongoDB multiple sort fields
> db.users.ensureIndex({instance_id: 1, first_name: 1, last_name: 1});
> db.users.find({ instance_id: ObjectId("4f22b9cf7e298452c5000001") }).sort({first_name: 1, last_name: 1}).explain()
{
"cursor" : "BtreeCursor instance_id_1_first_name_1_last_name_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"instance_id" : [
[
ObjectId("4f22b9cf7e298452c5000001"),
ObjectId("4f22b9cf7e298452c5000001")
]
],
"first_name" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"last_name" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment