<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 03:00:33 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[SERVER-2617] Indexing arrays and embedded JSON objects - use cardinality in query on multiple elements instead of first element in the match criteria (e.g. $all operator)</title>
                <link>https://jira.mongodb.org/browse/SERVER-2617</link>
                <project id="10000" key="SERVER">Core Server</project>
                    <description>&lt;p&gt;Related to this discussion (long, look at the end for most of the substance):&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://groups.google.com/d/topic/mongodb-user/mr1uz9PuEek/discussion&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://groups.google.com/d/topic/mongodb-user/mr1uz9PuEek/discussion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, MongoDB uses the first element in the $all operator to find the index and return the documents that are then scanned for the matches for the rest of the match criteria.  &lt;br/&gt;
Here is an example of the documents in the collection and a query, as well as the explain info:&lt;/p&gt;

&lt;p&gt;&amp;gt; r.ensureIndex(&lt;/p&gt;
{records:1}
&lt;p&gt;) &lt;br/&gt;
&amp;gt; r.find({records:{ $all:[ &lt;/p&gt;
{custid:456}
&lt;p&gt;,&lt;/p&gt;
{status:200}
&lt;p&gt; ] }})                                          &lt;br/&gt;
{ &quot;_id&quot; : ObjectId(&quot;4d6574f485904ac21ab43fca&quot;), &quot;bytes&quot; : 10, &quot;id&quot; : 1, &quot;records&quot; : [ &lt;/p&gt;
{ &quot;status&quot; : 200 }
&lt;p&gt;, &lt;/p&gt;
{ &quot;url&quot; : &quot;yahoo.com&quot; }
&lt;p&gt;, &lt;/p&gt;
{ &quot;custid&quot; : 456 }
&lt;p&gt; ] }&lt;br/&gt;
{ &quot;_id&quot; : ObjectId(&quot;4d6574fa85904ac21ab43fcb&quot;), &quot;bytes&quot; : 10, &quot;id&quot; : 1, &quot;records&quot; : [ &lt;/p&gt;
{ &quot;status&quot; : 200 }
&lt;p&gt;, &lt;/p&gt;
{ &quot;url&quot; : &quot;cnn.com&quot; }
&lt;p&gt;, &lt;/p&gt;
{ &quot;custid&quot; : 456 }
&lt;p&gt; ] }&lt;/p&gt;

&lt;p&gt;&amp;gt; r.find({records:{ $all:[ &lt;/p&gt;
{custid:456}
&lt;p&gt;,&lt;/p&gt;
{status:200}
&lt;p&gt; ] }}).explain()&lt;br/&gt;
{&lt;br/&gt;
    &quot;cursor&quot; : &quot;BtreeCursor records_1&quot;,&lt;br/&gt;
    &quot;nscanned&quot; : 4,&lt;br/&gt;
    &quot;nscannedObjects&quot; : 4,&lt;br/&gt;
    &quot;n&quot; : 2,&lt;br/&gt;
    &quot;millis&quot; : 0,&lt;br/&gt;
    &quot;indexBounds&quot; : {&lt;br/&gt;
        &quot;records&quot; : [&lt;br/&gt;
            [&lt;br/&gt;
                &lt;/p&gt;
{
                    &quot;custid&quot; : 456
                }
&lt;p&gt;,&lt;/p&gt;
                {
                    &quot;custid&quot; : 456
                }
&lt;p&gt;            ]&lt;br/&gt;
        ]&lt;br/&gt;
    }&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;According to how it was explained, the index used in this case (and similar queries) is always that of the first element of the query match array ($all), in this case on &quot;custid&quot; field, and then the documents returned from that index are scanned for matching of the rest of the query match terms (status:200 in this case).  Cardinality of the indexes of the indexes is not taken into account when selecting which index to apply for the query  - it is always just the index of the first field specified in $all.&lt;/p&gt;

&lt;p&gt;A couple suggestions &lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;evaluate all the query fields and select the most effective index (e.g. based on cardinality) instead of always the 1st element index.&lt;/li&gt;
	&lt;li&gt;use indexes for each step of the evaluation, i.e. after returning the first set of documents use index again to match against &quot;status&quot;:200 instead of performing the document scan for all subsequent elements.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;It would greatly enhance the effectiveness of performing queries on arbitrary number of embedded array elements.&lt;/p&gt;</description>
                <environment></environment>
        <key id="14865">SERVER-2617</key>
            <summary>Indexing arrays and embedded JSON objects - use cardinality in query on multiple elements instead of first element in the match criteria (e.g. $all operator)</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="3">Duplicate</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="alexatl">Aleksei Tolompoiko</reporter>
                        <labels>
                    </labels>
                <created>Wed, 23 Feb 2011 23:38:37 +0000</created>
                <updated>Fri, 15 Feb 2013 15:06:51 +0000</updated>
                            <resolved>Mon, 29 Oct 2012 04:21:48 +0000</resolved>
                                                                    <component>Index Maintenance</component>
                                        <votes>5</votes>
                                    <watches>8</watches>
                                                                                                                <comments>
                            <comment id="179956" author="aaron" created="Mon, 29 Oct 2012 04:21:48 +0000"  >&lt;p&gt;This feature is covered by &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-1000&quot; title=&quot;$all with query optimizer&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-1000&quot;&gt;&lt;del&gt;SERVER-1000&lt;/del&gt;&lt;/a&gt;.&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                            <outwardlinks description="duplicates">
                                        <issuelink>
            <issuekey id="11761">SERVER-1000</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                <customfield id="customfield_10050" key="com.atlassian.jira.toolkit:comments">
                        <customfieldname># Replies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_10055" key="com.atlassian.jira.ext.charting:firstresponsedate">
                        <customfieldname>Date of 1st Reply</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>Mon, 29 Oct 2012 04:21:48 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        11 years, 16 weeks, 2 days ago
    
                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_18254" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Dependencies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue><![CDATA[]]></customfieldvalue>


                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <customfield id="customfield_10057" key="com.atlassian.jira.toolkit:lastusercommented">
                        <customfieldname>Last comment by Customer</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>true</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_10056" key="com.atlassian.jira.toolkit:lastupdaterorcommenter">
                        <customfieldname>Last commenter</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>ian@mongodb.com</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_11151" key="com.atlassian.jira.toolkit:LastCommentDate">
                        <customfieldname>Last public comment date</customfieldname>
                        <customfieldvalues>
                            11 years, 16 weeks, 2 days ago
                        </customfieldvalues>
                    </customfield>
                                                                                                                        <customfield id="customfield_10000" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Old_Backport</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10000"><![CDATA[No]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_10051" key="com.atlassian.jira.toolkit:participants">
                        <customfieldname>Participants</customfieldname>
                        <customfieldvalues>
                                        <customfieldvalue>aaron</customfieldvalue>
            <customfieldvalue>alexatl</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hrp5dz:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hribl3:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>20522</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_23361" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Requested By</customfieldname>
                        <customfieldvalues>
                                

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_10053" key="com.atlassian.jira.ext.charting:timeinstatus">
                        <customfieldname>Time In Status</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <customfield id="customfield_22870" key="com.onresolve.jira.groovy.groovyrunner:scripted-field">
                        <customfieldname>Triagers</customfieldname>
                        <customfieldvalues>
                                

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_14350" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>serverRank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|ht06qv:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                    </customfields>
    </item>
</channel>
</rss>