Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-964

Get $elemMatch to correctly use the index on the array

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.3.2
    • Component/s: Querying
    • Labels:
      None
    • Environment:
      $ uname -a
      Linux server 2.6.24-27-generic #1 SMP Fri Mar 12 01:10:31 UTC 2010 i686 GNU/Linux

      Hello,

      I am using MongoDB to store at the moment 100,000 documents. Each document can have from 1 to 150 properties. I need to search by property range for any combination of properties. On average a document has 5 properties set. Here is for example 2 documents, the first has properties 1, 2 and 17 set and doc2 has properties 1, 3 and 45.

      doc1 =

      {prop1: 123.1, prop2: 345.2, prop17: 12.0}

      doc2 =

      {prop1: 124.1, prop3: 33455.2, prop45: 11232.0}

      The kind of queries needed are:

      db.collection.find({prop1: {$gt: 123.0, $lt: 250.0}, prop3: {$gt: 10.0}});

      It is not possible to create 150 indexes, one for each property, so Mathias (mstearn) suggested to store the properties this way with a single index on p:

      doc1 = {p: [

      {prop1: 123.1}

      ,

      {prop2: 345.2}

      ,

      {prop17: 12.0}

      ]}
      doc2 = {p: [

      {prop1: 124.1}

      ,

      {prop3: 33455.2}

      ,

      {prop45: 11232.0}

      ]}

      And to query this way:

      db.testarray.find({"p": {$elemMatch: {prop1: {$gt: 123.0, $lt: 250.0}, prop3: {$gt: 10.0}}}});

      But in that case, the index on p is not used and the complete collection is scanned.

      See the attached javascript setting up a small 10000 document collection with some test data.

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            loic Loïc d'Anterroches
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: