<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 07:59:35 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>[DOCS-9994] Unique field must be included to ensure sort order stability across multiple queries</title>
                <link>https://jira.mongodb.org/browse/DOCS-9994</link>
                <project id="10380" key="DOCS">Documentation</project>
                    <description>&lt;p&gt;There are number of reasons that the ordering of results with the same sort key might not be stable:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;The query execution stages for performing blocking sort operations in find/aggregation do not use a stable sort algorithm.&lt;/li&gt;
	&lt;li&gt;Unpredictability of network latency and getMore ordering for queries on sharded clusters may result in different orders. This is similar to the behavior described in &lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-27716&quot; title=&quot;mongos 3.2.10, sharded cluster, skip returns duplicates&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-27716&quot;&gt;&lt;del&gt;SERVER-27716&lt;/del&gt;&lt;/a&gt;, which was closed as Works as Designed.&lt;/li&gt;
	&lt;li&gt;Changes to the underlying storage. For example, document moves on the MMAPv1 storage engine can result in repeated COLLSCANs returning the data in different orders. Therefore, even if the blocking sort query execution algorithm &lt;em&gt;was&lt;/em&gt; stable, this still wouldn&apos;t be sufficient to guarantee consistent ordering of equal-keyed results.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Therefore, we should clarify that users who require a stable sort order across queries (eg pagination) add a unique field to their sort.&lt;/p&gt;</description>
                <environment></environment>
        <key id="363883">DOCS-9994</key>
            <summary>Unique field must be included to ensure sort order stability across multiple queries</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="9">Done</resolution>
                                        <assignee username="andrew.feierabend@mongodb.com">Andrew Feierabend</assignee>
                                    <reporter username="kelsey.schubert@mongodb.com">Kelsey Schubert</reporter>
                        <labels>
                    </labels>
                <created>Fri, 10 Mar 2017 22:49:52 +0000</created>
                <updated>Mon, 30 Oct 2023 21:15:48 +0000</updated>
                            <resolved>Mon, 7 Dec 2020 16:28:08 +0000</resolved>
                                                    <fixVersion>Server_Docs_20231030</fixVersion>
                                    <component>manual</component>
                    <component>Server</component>
                        <due>Fri, 4 Dec 2020 00:00:00 +0000</due>
                            <votes>0</votes>
                                    <watches>11</watches>
                                                                                                                <comments>
                            <comment id="3528667" author="xgen-internal-githook" created="Mon, 14 Dec 2020 18:09:21 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{&apos;name&apos;: &apos;Andrew Feierabend&apos;, &apos;email&apos;: &apos;andrew.feierabend@mongodb.com&apos;, &apos;username&apos;: &apos;andf-mongodb&apos;}
&lt;p&gt;Message: DOCSP-13532 extend &lt;a href=&quot;https://jira.mongodb.org/browse/DOCS-9994&quot; title=&quot;Unique field must be included to ensure sort order stability across multiple queries&quot; class=&quot;issue-link&quot; data-issue-key=&quot;DOCS-9994&quot;&gt;&lt;del&gt;DOCS-9994&lt;/del&gt;&lt;/a&gt; to cursor.limit() and cursor.skip()&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/docs/commit/5e75d59541afe0248ebd5675f15a85e5b5f876c6&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/docs/commit/5e75d59541afe0248ebd5675f15a85e5b5f876c6&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3528654" author="xgen-internal-githook" created="Mon, 14 Dec 2020 18:05:57 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{&apos;name&apos;: &apos;Andrew Feierabend&apos;, &apos;email&apos;: &apos;andrew.feierabend@mongodb.com&apos;, &apos;username&apos;: &apos;andf-mongodb&apos;}
&lt;p&gt;Message: DOCSP-13532 extend &lt;a href=&quot;https://jira.mongodb.org/browse/DOCS-9994&quot; title=&quot;Unique field must be included to ensure sort order stability across multiple queries&quot; class=&quot;issue-link&quot; data-issue-key=&quot;DOCS-9994&quot;&gt;&lt;del&gt;DOCS-9994&lt;/del&gt;&lt;/a&gt; to cursor.limit() and cursor.skip()&lt;br/&gt;
Branch: v5.0&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/docs/commit/1cfaf6cbbb9a7ab363796d6549fd3811790fa142&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/docs/commit/1cfaf6cbbb9a7ab363796d6549fd3811790fa142&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3518750" author="xgen-internal-githook" created="Mon, 7 Dec 2020 16:25:57 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{&apos;name&apos;: &apos;Andrew Feierabend&apos;, &apos;email&apos;: &apos;andrew.feierabend@mongodb.com&apos;, &apos;username&apos;: &apos;andf-mongodb&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/DOCS-9994&quot; title=&quot;Unique field must be included to ensure sort order stability across multiple queries&quot; class=&quot;issue-link&quot; data-issue-key=&quot;DOCS-9994&quot;&gt;&lt;del&gt;DOCS-9994&lt;/del&gt;&lt;/a&gt;,DOCSP-13487 document sort stability change in v4.4&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/docs/commit/c7567f88df1f6ba193d2f9ed7c9ffbe034073fdf&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/docs/commit/c7567f88df1f6ba193d2f9ed7c9ffbe034073fdf&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3518744" author="xgen-internal-githook" created="Mon, 7 Dec 2020 16:24:51 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{&apos;name&apos;: &apos;Andrew Feierabend&apos;, &apos;email&apos;: &apos;andrew.feierabend@mongodb.com&apos;, &apos;username&apos;: &apos;andf-mongodb&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/DOCS-9994&quot; title=&quot;Unique field must be included to ensure sort order stability across multiple queries&quot; class=&quot;issue-link&quot; data-issue-key=&quot;DOCS-9994&quot;&gt;&lt;del&gt;DOCS-9994&lt;/del&gt;&lt;/a&gt;,DOCSP-13487 document sort stability change in v4.4&lt;br/&gt;
Branch: v5.0&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/docs/commit/0a5899664db191a2da72a1a23a963f780ee115d9&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/docs/commit/0a5899664db191a2da72a1a23a963f780ee115d9&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="2053487" author="david.storch" created="Tue, 6 Nov 2018 18:23:21 +0000"  >&lt;p&gt;&lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=ravind.kumar&quot; class=&quot;user-hover&quot; rel=&quot;ravind.kumar&quot;&gt;ravind.kumar&lt;/a&gt;, it looks like your questions have been answered, but happy to help if you need any more clarification. The bottom line is that any query which requests a sort in MQL (find command sort, the $sort stage, sort for findAndModify) does not guarantee stability within the set of documents that have the same sort key. Assuming you have (document, sort key) pairs (d1, 2), (d2, 2), (d3, 1), (d4, 8), it is guaranteed that the order for an ascending sort is either d3, d1, d2, d4 or d3, d2, d1, d4. Since d1 and d2 are equal, they may come back in either order. Applications that require an exact ordering must ensure that no two documents have the same sort key.&lt;/p&gt;</comment>
                            <comment id="2050405" author="thomas.schubert" created="Fri, 2 Nov 2018 16:57:47 +0000"  >&lt;blockquote&gt;
&lt;p&gt;is this also true for WiredTiger?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;No&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;given that sorts can take advantage of indexes, is the implication here something like a compound unique index on the sort field + one additional field would be the ideal support for a consistently ordered sort?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;yes, there should be a compound index of the desired sort + one unique field at the end, and then the user should sort against this entire pattern. E.g. if they wanted to consistent sort on {a:1, b:1}, they could created an index and sort on  {a:1, b:1, _id: 1}.&lt;/p&gt;</comment>
                            <comment id="2050340" author="christopher.harris" created="Fri, 2 Nov 2018 15:50:16 +0000"  >&lt;p&gt;Regarding the first question, a non-blocking sort uses an index.  Per &lt;a href=&quot;https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;&quot;Use Indexes to Sort Query Results&quot;&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;In a blocking SORT, all input must be consumed by the sort step before it can produce output. In a non-blocking, or &lt;em&gt;indexed&lt;/em&gt; sort, the sort step scans the index to produce results in the requested order.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;It was also the topic of &lt;a href=&quot;https://groups.google.com/forum/#!topic/mongodb-dev/j2Mp5zNU7T4&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;this Google Group discussion&lt;/a&gt; a few years ago.&lt;/p&gt;
</comment>
                            <comment id="2050324" author="ravind.kumar" created="Fri, 2 Nov 2018 15:39:01 +0000"  >&lt;p&gt;Taking this on from Andrew. A few questions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
	&lt;li&gt;The query execution stages for performing blocking sort operations in find/aggregation do not use a stable sort algorithm.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;What defines &apos;blocking sort operations&apos; ? We document &lt;tt&gt;$sort&lt;/tt&gt; for agg and &lt;tt&gt;cursor.sort()&lt;/tt&gt; for queries - are those always blocking, or are there specific scenarios in which they block? cc &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=david.storch&quot; class=&quot;user-hover&quot; rel=&quot;david.storch&quot;&gt;david.storch&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Changes to the underlying storage. For example, document moves on the MMAPv1 storage engine can result in repeated COLLSCANs returning the data in different orders. Therefore, even if the blocking sort query execution algorithm was stable, this still wouldn&apos;t be sufficient to guarantee consistent ordering of equal-keyed results.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;is this also true for WiredTiger? cc &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=alexander.gorrod%40mongodb.com&quot; class=&quot;user-hover&quot; rel=&quot;alexander.gorrod@mongodb.com&quot;&gt;alexander.gorrod@mongodb.com&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Therefore, we should clarify that users who require a stable sort order across queries (eg pagination) add a unique field to their sort.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;given that sorts can take advantage of indexes, is the implication here something like a compound unique index on the sort field + one additional field would be the ideal support for a consistently ordered sort? &lt;/p&gt;</comment>
                            <comment id="1984833" author="andrew.aldridge@10gen.com" created="Fri, 24 Aug 2018 14:29:11 +0000"  >&lt;p&gt;I will take a look today!&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10320">
                    <name>Documented</name>
                                            <outwardlinks description="documents">
                                        <issuelink>
            <issuekey id="361440">SERVER-28195</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                            <issuelinktype id="10423">
                    <name>Gantt End to End</name>
                                                                <inwardlinks description="has to be finished together with">
                                                        </inwardlinks>
                                    </issuelinktype>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="1510789">SERVER-51498</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="2454739">SERVER-81571</issuekey>
        </issuelink>
                            </outwardlinks>
                                                                <inwardlinks description="is related to">
                                                        </inwardlinks>
                                    </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                <customfield id="customfield_10050" key="com.atlassian.jira.toolkit:comments">
                        <customfieldname># Replies</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_13552" key="com.go2group.jira.plugin.crm:crm_generic_field">
                        <customfieldname>Case</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue><![CDATA[[500A000000bS61mIAC]]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_10055" key="com.atlassian.jira.ext.charting:firstresponsedate">
                        <customfieldname>Date of 1st Reply</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>Fri, 24 Aug 2018 10:57:42 +0000</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10052" key="com.atlassian.jira.toolkit:dayslastcommented">
                        <customfieldname>Days since reply</customfieldname>
                        <customfieldvalues>
                                        3 years, 8 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_10857" key="com.pyxis.greenhopper.jira:gh-epic-link">
                        <customfieldname>Epic Link</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>DOCSP-11701</customfieldvalue>
                        </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>emet.ozar@mongodb.com</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_11151" key="com.atlassian.jira.toolkit:LastCommentDate">
                        <customfieldname>Last public comment date</customfieldname>
                        <customfieldvalues>
                            3 years, 8 weeks, 2 days ago
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                            <customfield id="customfield_10051" key="com.atlassian.jira.toolkit:participants">
                        <customfieldname>Participants</customfieldname>
                        <customfieldvalues>
                                        <customfieldvalue>andrew.aldridge@mongodb.com</customfieldvalue>
            <customfieldvalue>andrew.feierabend@mongodb.com</customfieldvalue>
            <customfieldvalue>christopher.harris@mongodb.com</customfieldvalue>
            <customfieldvalue>david.storch@mongodb.com</customfieldvalue>
            <customfieldvalue>xgen-internal-githook</customfieldvalue>
            <customfieldvalue>kelsey.schubert@mongodb.com</customfieldvalue>
            <customfieldvalue>ravind.kumar</customfieldvalue>
    
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                        <customfield id="customfield_14254" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Product Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|ht43c7:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hswlcv:</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_10557" key="com.pyxis.greenhopper.jira:gh-sprint">
                        <customfieldname>Sprint</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue id="1324">KANBAN BUCKET</customfieldvalue>
    <customfieldvalue id="4539">ServerDocs2020: Dec1 - Dec8</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                        <customfield id="customfield_10555" key="com.atlassian.jira.plugin.system.customfieldtypes:float">
                        <customfieldname>Story Points</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>4.0</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_10053" key="com.atlassian.jira.ext.charting:timeinstatus">
                        <customfieldname>Time In Status</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_14350" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>serverRank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>1|hrr9bz:</customfieldvalue>

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