I believe I stumbed on the same problem when investigating some
performance issue only when using the Mongo PHP driver.
The Mongo PHP driver will wrap the query in a $query construct if
there is any option used (addOption)
The following behaviour is same with Mongod 1.6.5 and 2.0.0
A first query working as expected with the index:
> db.news_item.find()._addSpecial('query', {actor_id:32126}).explain();
{
"cursor" : "BtreeCursor actor_id_1",
"nscanned" : 10,
"nscannedObjects" : 10,
"n" : 10,
"millis" : 0,
"indexBounds" : {
"actor_id" : [
[
32126,
32126
]
]
}
}
127.0.0.1:35273 -->>
127.0.0.1:27017 maresidence.news_item 96 bytes
id:c569863a
3312027194
query: { query: { actor_id: 32126.0 }, $explain: true } ntoreturn: 0
ntoskip: 0
127.0.0.1:27017 <<--
127.0.0.1:35273 426 bytes id:7a6959b3
2053724595 -
3312027194
reply n:1 cursorId: 0
{ cursor: "BtreeCursor actor_id_1", nscanned: 10, nscannedObjects:
10, n: 10, millis: 0, indexBounds: { actor_id: { 0: { 0: 32126.0, 1:
32126.0 } } }, allPlans: { 0: { cursor: "BtreeCursor actor_id_1",
indexBounds: { actor_id: { 0: { 0: 32126.0, 1: 32126.0 } } } } },
oldPlan: { cursor: "BtreeCursor actor_id_1", indexBounds: { actor_id:
{ 0: { 0: 32126.0, 1: 32126.0 } } } } }
Another query with the $query construct, index is bypassed:
> db.news_item.find({$query: {actor_id:32126}}).explain();
{
"cursor" : "BasicCursor",
"nscanned" : 92154,
"nscannedObjects" : 92154,
"n" : 0,
"millis" : 173,
"indexBounds" : {
}
}
127.0.0.1:35273 -->>
127.0.0.1:27017 maresidence.news_item 109
bytes id:c569863b
3312027195
query: { query: { $query: { actor_id: 32126.0 } }, $explain: true }
ntoreturn: 0 ntoskip: 0
127.0.0.1:27017 <<--
127.0.0.1:35273 258 bytes id:7a6959b4
2053724596 -
3312027195
reply n:1 cursorId: 0
{ cursor: "BasicCursor", nscanned: 92154, nscannedObjects: 92154, n:
0, millis: 173, indexBounds: {}, allPlans: { 0: { cursor:
"BasicCursor", indexBounds: {} } }, oldPlan: { cursor: "BasicCursor",
indexBounds: {} } }
Both queries send back the expected results, the $query one is much
slower.
And as a bonus:
> db.news_item.find()._addSpecial('query', {actor_id:32126}).count();
10
> db.news_item.find({}, {actor_id:32126})._addSpecial('query', {actor_id:32126})
{ "_id" : ObjectId("4c638c846fd62f7807930000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638c846fd62f7807970000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638ca66fd62f7807780300"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638cfa6fd62f7807490b00"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638d116fd62f7807e40c00"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638f236fd62f78078e2400"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638f346fd62f7807462500"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c69533e6fd62f5d1c480800"), "actor_id" : 32126 }
{ "_id" : ObjectId("4d6a7e9ff43cf9ea0f000000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4d8087cc8ac5138218010000"), "actor_id" : 32126 }
>
> db.news_item.find({$query: {actor_id:32126}}).count()
0
> db.news_item.find({$query: {actor_id:32126}}, {actor_id:1})
{ "_id" : ObjectId("4c638c846fd62f7807930000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638c846fd62f7807970000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638ca66fd62f7807780300"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638cfa6fd62f7807490b00"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638d116fd62f7807e40c00"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638f236fd62f78078e2400"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c638f346fd62f7807462500"), "actor_id" : 32126 }
{ "_id" : ObjectId("4c69533e6fd62f5d1c480800"), "actor_id" : 32126 }
{ "_id" : ObjectId("4d6a7e9ff43cf9ea0f000000"), "actor_id" : 32126 }
{ "_id" : ObjectId("4d8087cc8ac5138218010000"), "actor_id" : 32126 }
count() is wrong with $query construct.
Best regards,
Francois