I was creating my first app using MongoDB. Created index for a field, and tried a find query with $regex param, launched in a shell
> db.foo.find({A:{$regex:'BLABLA!25500[0-9]'}}).explain()
{
"cursor" : "BtreeCursor A_1 multi",
"nscanned" : 500001,
"nscannedObjects" : 10,
"n" : 10,
"millis" : 956,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"A" : [
[
"",
{
}
],
[
/BLABLA!25500[0-9]/,
/BLABLA!25500[0-9]/
]
]
}
}
It's very strange, because when i'm launching the same query, but with no index in collection, the performance is much better.
> db.foo.find({A:{$regex:'BLABLA!25500[0-9]'}}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 500002,
"nscannedObjects" : 500002,
"n" : 10,
"millis" : 531,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
Obviously, searching a field with index without regex is working much faster(i.e. searching document with constant field) , but i'm really interested in reason of such behavior.