How does MongoDB choose the index to use?

28 views
Skip to first unread message

Bogdan Ciobanu

unread,
Jun 18, 2014, 11:05:15 AM6/18/14
to mongod...@googlegroups.com
Hi,
I am learning to use MongoDB on a collection containing documents with the following structure:
{
   
"_id" : ObjectId("53a18d636171cfe49d7647b6"),
   
"datetime" : "2011-01-01 00:00:00",
   
"season" : 1,
   
"holiday" : 0,
   
"workingday" : 0,
   
"weather" : 1,
   
"temp" : 9.84,
   
"atemp" : 14.395,
   
"humidity" : 81,
   
"windspeed" : 0,
   
"casual" : 3,
   
"registered" : 13,
   
"count" : 16
}
and I have defined 3 indexes:
db.collection.ensureIndex({temp:1})
db
.collection.ensureIndex({count:1})
db
.collection.ensureIndex({temp:1, count:1})

Based on the fact that the documentation specifies that "MongoDB automatically uses an index that covers a query when possible" [1], I was expecting the following query to be indexOnly:
db.bikes.find({temp: {$gt:23}}, {temp:1, count:1, _id: 0})

Unfortunately, it uses the ({temp: 1}) index and it obviously doesn't cover the query.

Is there any logic under the index used and in what way is Mongo using an index that covers a query?

[1] - http://docs.mongodb.org/manual/tutorial/create-indexes-to-support-queries/#create-indexes-that-support-covered-queries

Asya Kamsky

unread,
Jun 19, 2014, 12:00:29 AM6/19/14
to mongodb-user
Are you using 2.6.1?   If so I would try to use 2.6.2 and see if the behavior changes.

If not, then run the same query with explain(true) and post all the plans here - I'm assuming this query actually matches more than one document, right?

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c1f6c01c-5bed-428e-89b2-a75d7be9c04b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages