Database Reference
In-Depth Information
"name" : "_id_",
"ns" : "tutorial.numbers",
"key" : {
"_id" : 1
}
},
{
"_id" : ObjectId("4bfc646b2f95a56b5581efd3"),
"ns" : "tutorial.numbers",
"key" : {
"num" : 1
},
"name" : "num_1"
}
]
The collection now has two indexes. The first is the standard
_id
index that's automat-
ically built for every collection; the second is the index you just created on
num
.
If you run your query with the
explain()
method, you'll now see a dramatic differ-
ence in query response time, as shown in the next listing.
Listing 2.2
explain()
output for an indexed query
> db.numbers.find({num: {"$gt": 199995 }}).explain()
{
"cursor" : "BtreeCursor num_1",
"indexBounds" : [
[
{
"num" : 199995
},
{
"num" : 1.7976931348623157e+308
}
]
],
"nscanned" : 5,
"nscannedObjects" : 4,
"n" : 4,
"millis" : 0
}
Now that the query utilizes the index on
num
, it scans only the five documents pertain-
ing to the query. This reduces the total time to serve the query from 171 ms to less
than 1 ms.
If this example intrigues you, be sure to check out chapter 7, which is devoted to
indexing and query optimization. Now we'll proceed now to look at the basic adminis-
trative commands required to get information about our MongoDB instance. You'll
also learn some techniques for getting help from the shell, which will aid in mastering
the various shell commands.