I save game files in MongoDB. Among other things, the documents contain the player’s name (name), the end time of the game (endMS) and the type of game (type). A type can have one of five different values.
I need to search for all finished games by a player sorted by the end time of the game and for all finished games by a player with a certain type of game, also sorted by the end time of the game.
Examples for both queries:
db.games.find ({name: "Stefan", endMS: {$ Gt: 0}}). Sorts ({endMS: -1})
and
db.games.find ({name: "Stefan", type: "li", endMS: {$ Gt: 0}}). Sorts ({endMS: -1})
You can use indexes
db.games.ensureIndex ({name: 1, endMS: -1})
and
db.games.ensureIndex ({name: 1, type: 1, endMS: -1})
for quick access.
:
db.games.ensureIndex({: 1, endMS: -1, : 1})
- . , Mongo , , , , , "" . .
, explain(), MongoDB , "scanAndOrder".
db.games.find({: "" , : "", endMS: {$ Gt: 0}}) ({endMS: -1}).. ()
{
"cursor" : "BtreeCursor name_1_endMS_-1_type_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 22,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 25,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"Stefan",
"Stefan"
]
],
"endMS" : [
[
Infinity,
0
]
],
"type" : [
[
"bli",
"bli"
]
]
},
"server" : "localhost:27017",
"filterSet" : false
}
nscannedObjects nscanned , , , Mongo scanAndOrder: true.
:
"scanAndOrder - , , : MongoDB ".
, , , .
, MongoDB scanAndOrder ?