[DOCS-958] Clarification on $regex use of indexes Created: 07/Jan/13  Updated: 30/Oct/23  Resolved: 08/Jan/13

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Task Priority: Major - P3
Reporter: Stennie Steneker (Inactive) Assignee: Sam Kleinman (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 11 years, 6 weeks, 2 days ago

 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.



 Comments   
Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2013-01-08T03:52:31Z', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: merge: DOCS-958
Branch: master
https://github.com/mongodb/docs/commit/1fcb424390475c2ffc73a1becfb2c91a0963bd0a

Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2013-01-07T23:21:15Z', u'email': u'stennie@10gen.com', u'name': u'Stephen Steneker'}

Message: DOCS-958: Clarification on $regex use of indexes
Branch: master
https://github.com/mongodb/docs/commit/ba87721e959af800af8f48e8633339e7d359e93f

Generated at Thu Feb 08 07:39:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.