Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-958

Clarification on $regex use of indexes

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • manual
    • 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.

      Attachments

        Activity

          People

            sam.kleinman Sam Kleinman (Inactive)
            stephen.steneker@mongodb.com Stennie Steneker (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              11 years, 6 weeks, 2 days ago