Slide 12
Slide 12 text
Ranges, Sorts, and Index Key Order
11
carsOwned country
carsOwned
country
document visitation order
unsorted
1 2 1 2
MongoDB performs a full, in-memory scanAndOrder
{ name: “Bob”,
country: “A”,
carsOwned: 1 }
{ ...“Charlie”,
... “A”,
... 2 }
{ ...“Holger”,
... “G”,
... 1 }
{ ...“Dietmar”,
... “G”,
... 2 }
sorted
Visitation order satisfies sort; no scanAndOrder needed
{ ...“Charlie”,
... “A”,
... 2 }
{ ...“Dietmar”,
... “G”,
... 2 }
A G
{ name: “Bob”,
country: “A”,
carsOwned: 1 }
{ ...“Holger”,
... “G”,
... 1 }
1 2
1
A G G
1 2
A
optimal index: {carsOwned: 1, country: 1}
document visitation order
suboptimal index: {country: 1, carsOwned: 1}
db.drivers.find({country: {$in: [”A”, “G”]}).sort({carsOwned: 1})
Diagram at bit.ly/mongoindex