With MongoDB 2.6.5
I have a collection of documents with this structure :
{
"_id" : ObjectId("5485cd0c6b0f96004220e414"),
"exampleList" : [{
"Value" : "uri:obj:id:1258477.479.129403280"
},{
"Value" : "uri:obj:id:1258477.542.542541247"
}, {
"Value" : "uri:obj:id:1258477.365.455255425"
}
[...]
{
"Value" : "uri:obj:id:1258477.147.855556255"
}]
}
I have set a multikey index on "exampleList.Value".
I want to request it with a regex of type "starts with" but it can be very slow according to the regex. Shorter is the fix part of the regex (more results), slower is the treatment.
Demo with a 100 millions documents collection "myCollection" :
Fastest execution (immediate):
> db.myCollection.count({"exampleList.Value":{$regex:/^uri:obj:id:1258477\.479\.129403280$/}})
156
Fast execution (some seconds):
> db.myCollection.count({"exampleList.Value":{$regex:/^uri:obj:id:1258477\.479\.129.*$/}})
502
Slower execution (some seconds) :
> db.myCollection.count({"exampleList.Value":{$regex:/^uri:obj:id:1258477\.479\.1.*$/}})
40947
Slow execution (~2 minutes)
> db.myCollection.count({"exampleList.Value":{$regex:/^uri:obj:id:1258477\.479.*$/}})
342275
Very very slooooowww execution (not terminated, several minutes)
> db.myCollection.count({"exampleList.Value":{$regex:/^uri:obj:id:1258477\.47.*$/}})
I don't understand why the time of the treatment is not the same in all these queries.
explainand I bet thatnscannedincreases.