<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 04:29:20 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-32151] Would like the ability to use the index for sorting an array of sub-documents (aggregation pipeline)</title>
                <link>https://jira.mongodb.org/browse/SERVER-32151</link>
                <project id="10000" key="SERVER">Core Server</project>
                    <description>&lt;p&gt;Correct me if I&apos;m wrong, I believe that it is currently impossible to sort an array of sub-documents while using its index.&lt;/p&gt;

&lt;p&gt;Take for example this collection of exactly 1 document (and imagine it has 10,000 sub-documents in the `readers` array.) The collection has an index on `readers.name`.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;{_id: 1, book: &quot;The Great Book&quot;, readers: [&lt;br/&gt;
{_id: 1, name: &quot;Joe&quot;, sex: &quot;male&quot;},{_id: 2, name: &quot;Jane&quot;, sex: &quot;female&quot;}]}&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;As soon as `$unwind` is used in an aggregation pipeline, the aggregation pipeline stops using all indexes.&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;My first step in the aggregation pipeline would be to use `$match` to match `
{book: &quot;The Great Book}
&lt;p&gt;`.&lt;/p&gt;&lt;/li&gt;
	&lt;li&gt;My second step in the aggregation pipeline would be to `$unwind` the `readers`.  ====&amp;gt; (So, if I understand correctly, I won&apos;t be able to make use of indexes after this step!?)&lt;/li&gt;
	&lt;li&gt;My third step in the aggregation pipeline would be to sort by `&quot;readers.name&quot;`.  (If I understand correctly, this third step cannot make use of an index?!)&amp;lt;/p&amp;gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Is there a different way (that I cannot see) of doing this sort without clogging the memory (by not using the index)?&lt;/p&gt;</description>
                <environment></environment>
        <key id="467229">SERVER-32151</key>
            <summary>Would like the ability to use the index for sorting an array of sub-documents (aggregation pipeline)</summary>
                <type id="2" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14711&amp;avatarType=issuetype">New Feature</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="13202">Works as Designed</resolution>
                                        <assignee username="mark.agarunov">Mark Agarunov</assignee>
                                    <reporter username="emilio911">Emil Arsa</reporter>
                        <labels>
                    </labels>
                <created>Fri, 1 Dec 2017 23:36:58 +0000</created>
                <updated>Fri, 27 Oct 2023 13:54:04 +0000</updated>
                            <resolved>Mon, 4 Dec 2017 19:10:06 +0000</resolved>
                                                                                        <votes>0</votes>
                                    <watches>8</watches>
                                                                                                                <comments>
                            <comment id="1741777" author="mark.agarunov" created="Mon, 4 Dec 2017 18:06:52 +0000"  >&lt;p&gt;Hello &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=emilio911&quot; class=&quot;user-hover&quot; rel=&quot;emilio911&quot;&gt;emilio911&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for the report. MongoDB indexes only support sorting documents, not array elements, and that&#8217;s why the index cannot be used after the $unwind stage to sort the elements of the array. This is because after the &lt;tt&gt;$unwind&lt;/tt&gt; stage, the documents returned no longer match the layout of the documents stored in the collection and index.&lt;/p&gt;

&lt;p&gt;Depending on your needs, some options would be to insert these elements at the top level in the collection, a document for each &lt;tt&gt;reader&lt;/tt&gt;, which would allow sorting with the index, and would give the same output as unwinding the array. You could then use &lt;tt&gt;$group&lt;/tt&gt; and &lt;tt&gt;$project&lt;/tt&gt; to adjust the results to your desired schema. Alternatively, you could sort the documents on insertion instead of with the aggregation &lt;a href=&quot;https://docs.mongodb.com/manual/reference/operator/update/sort/#sort-array-of-documents-by-a-field-in-the-documents&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;by using the &lt;tt&gt;$push&lt;/tt&gt;,  &lt;tt&gt;$each&lt;/tt&gt;, and &lt;tt&gt;$sort&lt;/tt&gt; stages&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the &lt;a href=&quot;https://groups.google.com/group/mongodb-user&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;mongodb-user group&lt;/a&gt; or &lt;a href=&quot;https://stackoverflow.com/questions/tagged/mongodb&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;Stack Overflow with the &lt;tt&gt;mongodb&lt;/tt&gt; tag&lt;/a&gt;. A question like this involving more discussion would be best posted on the mongodb-user group.&lt;/p&gt;

&lt;p&gt;Thanks,&lt;br/&gt;
Mark&lt;/p&gt;</comment>
                    </comments>
                    <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, 4 Dec 2017 18:06:52 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        6 years, 10 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>luke.bonanomi@mongodb.com</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_11151" key="com.atlassian.jira.toolkit:LastCommentDate">
                        <customfieldname>Last public comment date</customfieldname>
                        <customfieldvalues>
                            6 years, 10 weeks, 2 days ago
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                    <customfield id="customfield_10051" key="com.atlassian.jira.toolkit:participants">
                        <customfieldname>Participants</customfieldname>
                        <customfieldvalues>
                                        <customfieldvalue>emilio911</customfieldvalue>
            <customfieldvalue>mark.agarunov</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|htlfjb:</customfieldvalue>

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

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</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|htl1nb:</customfieldvalue>

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