<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 03:03:00 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-3415] Sorting by nested objects in an array</title>
                <link>https://jira.mongodb.org/browse/SERVER-3415</link>
                <project id="10000" key="SERVER">Core Server</project>
                    <description>&lt;p&gt;We have a document paraphrased as follows:&lt;/p&gt;

&lt;p&gt;user {&lt;br/&gt;
 emailAddress: &quot;someEmail&quot;,&lt;br/&gt;
 groups: [&lt;br/&gt;
    &lt;/p&gt;
{ groupName : &quot;SomeName&quot;, groupJoinedDate: someDate}
&lt;p&gt;,&lt;/p&gt;
    { groupName : &quot;SomeOtherName&quot;, groupJoinedDate: someOtherDate}
&lt;p&gt;    ...&lt;br/&gt;
]&lt;br/&gt;
...&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;The query that we need to run would be something like:&lt;/p&gt;

&lt;p&gt;db.user.find(&lt;/p&gt;
{groups.groupName : &quot;SomeName&quot;}
&lt;p&gt;).sort(&lt;/p&gt;
{groups.dateJoined : 1}
&lt;p&gt;)&lt;/p&gt;

&lt;p&gt;The issue would be that sorting by groups.dateJoined needs to refer to the specific group that we are trying to &apos;find&apos;. It may be that the user has many tens of groups against their record and we would need to sort ONLY by the joined date of the group object we are specifically referring too, in the above example we want to sort user records by the date they joined &quot;SomeName&quot;.&lt;/p&gt;

&lt;p&gt;We think we get this to work by using compound indexes on the groupName + groupJoinedDate. One index for each direction, ie&lt;/p&gt;

&lt;p&gt;db.user.ensureIndex(&lt;/p&gt;
{&quot;group.name&quot; : 1, &quot;group.dateJoined&quot; : 1}
&lt;p&gt;)&lt;br/&gt;
db.user.ensureIndex(&lt;/p&gt;
{&quot;group.groupName&quot; : 1, &quot;group.dateJoined&quot; : -1}
&lt;p&gt;)&lt;/p&gt;

&lt;p&gt;Gives us sorted by ascending by default and we can force a descending query by adding .sort(&lt;/p&gt;
{&quot;groups.dateJoined&quot; : -1 }
&lt;p&gt; to the query.&lt;/p&gt;

&lt;p&gt;Does this sound like a legitimate way to accomplish this? Also is there a performance aspect to this query that I would need to be aware of?&lt;/p&gt;

&lt;p&gt;Any thoughts or design suggestions welcome.&lt;/p&gt;

</description>
                <environment></environment>
        <key id="19411">SERVER-3415</key>
            <summary>Sorting by nested objects in an array</summary>
                <type id="6" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14720&amp;avatarType=issuetype">Question</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="9">Done</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="matwall">Mat Wall</reporter>
                        <labels>
                            <label>indexing</label>
                            <label>performance</label>
                            <label>query</label>
                    </labels>
                <created>Tue, 12 Jul 2011 11:14:18 +0000</created>
                <updated>Mon, 29 Aug 2011 15:56:55 +0000</updated>
                            <resolved>Tue, 12 Jul 2011 11:40:36 +0000</resolved>
                                    <version>1.8.2</version>
                                                    <component>Index Maintenance</component>
                    <component>Querying</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="41878" author="matwall" created="Tue, 12 Jul 2011 13:43:54 +0000"  >&lt;p&gt;OK thanks.&lt;/p&gt;

&lt;p&gt;I have a second index on the group.name which enforces uniqueness which should alleviate some of the issues you refer to with multiple matching.&lt;/p&gt;

&lt;p&gt;So just to confirm, &lt;/p&gt;

&lt;p&gt;(1) the solution above is OK&lt;br/&gt;
(2) I only need one index to allow sorting in either direction&lt;br/&gt;
(3) I can do db.collection.find(&lt;/p&gt;
{&quot;group.name&quot; : &quot;SomeName}
&lt;p&gt;).sort(&quot;group.dateJoined&quot; : 1) AND db.collection.find(&lt;/p&gt;
{&quot;group.name&quot; : &quot;SomeName}
&lt;p&gt;).sort(&quot;group.dateJoined&quot; : -1) for sorting purposes (although case 1 is unnecessary)&lt;/p&gt;

&lt;p&gt;Many thanks&lt;/p&gt;</comment>
                            <comment id="41858" author="scotthernandez" created="Tue, 12 Jul 2011 11:40:24 +0000"  >&lt;p&gt;Yes, this should work for a single groupName match per document. If you get multiple matches per document (which is where all those index entries point) then it will not be so predictable what sort you will get.&lt;/p&gt;

&lt;p&gt;You don&apos;t need two index; The index can be used forward or in reverse.&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>2.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_10055" key="com.atlassian.jira.ext.charting:firstresponsedate">
                        <customfieldname>Date of 1st Reply</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>Tue, 12 Jul 2011 11:40:24 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        12 years, 32 weeks, 1 day 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>
                            12 years, 32 weeks, 1 day 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>matwall</customfieldvalue>
            <customfieldvalue>scotthernandez</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hrovv3:</customfieldvalue>

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

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

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