Details
-
Task
-
Resolution: Done
-
Major - P3
-
None
-
None
Description
From the $regex documentation at : http://docs.mongodb.org/manual/reference/operator/regex/
$regex uses indexes only when the regular expression has an anchor for the beginning (i.e. ^) of a string. Additionally, while /^a/, /^a./, and /^a.$/ are equivalent, they have different performance characteristics. All of these expressions use an index if an appropriate index exists; however, /^a./, and /^a.$/ are slower. /^a/ can stop scanning after matching the prefix.
The first sentence implies that $regex won't use an index unless it starts with ^, and doesn't mention case sensitivity.
I believe this would be more correctly phrased as:
$regex can only use an index efficiently when the regular expression has an anchor for the beginning (i.e. ^) of a string and is a case-sensitive match. Additionally, while /^a/, /^a./, and /^a.$/ match equivalent strings, they have different performance characteristics. All of these expressions use an index if an appropriate index exists; however, /^a./, and /^a.$/ are slower. /^a/ can stop scanning after matching the prefix.