<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:36:45 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>[GODRIVER-1610] bson.M can&apos;t be used as a sort document</title>
                <link>https://jira.mongodb.org/browse/GODRIVER-1610</link>
                <project id="14289" key="GODRIVER">Go Driver</project>
                    <description>&lt;p&gt;type AB struct &lt;/p&gt;
{
 A int32 bson:&quot;A&quot;
 B int32 bson:&quot;B&quot;
}

&lt;p&gt;func TestSort(t *testing.T) {&lt;br/&gt;
 mongodb.Init()&lt;br/&gt;
 defer mongodb.Close()&lt;/p&gt;

&lt;p&gt; mongodb.GetTrainingDB().Collection(&quot;TestSort&quot;).Drop(context.Background())&lt;/p&gt;

&lt;p&gt; for i := 0; i &amp;lt; 5; i++ {&lt;br/&gt;
  mongodb.GetTrainingDB().Collection(&quot;TestSort&quot;).InsertOne(context.Background(), bson.M&lt;/p&gt;
{&quot;A&quot;: i, &quot;B&quot;: rand.Int31n(10)}
&lt;p&gt;)&lt;br/&gt;
  mongodb.GetTrainingDB().Collection(&quot;TestSort&quot;).InsertOne(context.Background(), bson.M&lt;/p&gt;
{&quot;A&quot;: i, &quot;B&quot;: rand.Int31n(10)}
&lt;p&gt;)&lt;br/&gt;
 }&lt;/p&gt;

&lt;p&gt; for i := 0; i &amp;lt; 100; i++ {&lt;br/&gt;
  sortABList := []AB{}&lt;br/&gt;
  theOption := options.Find().SetSort(bson.M&lt;/p&gt;
{&quot;A&quot;: -1, &quot;B&quot;: -1}
&lt;p&gt;)&lt;br/&gt;
  theCur, _ := mongodb.GetTrainingDB().Collection(&quot;TestSort&quot;).Find(context.Background(), bson.M{}, theOption)&lt;br/&gt;
  defer theCur.Close(context.Background())&lt;/p&gt;

&lt;p&gt;  for theCur.Next(context.Background()) &lt;/p&gt;
{
   var theValue AB
   theCur.Decode(&amp;amp;theValue)

   sortABList = append(sortABList, theValue)
  }

&lt;p&gt;  for i := 0; i &amp;lt; len(sortABList)-1; i++ {&lt;br/&gt;
   fmt.Println(sortABList&lt;span class=&quot;error&quot;&gt;&amp;#91;i&amp;#93;&lt;/span&gt;)&lt;br/&gt;
   if sortABList&lt;span class=&quot;error&quot;&gt;&amp;#91;i&amp;#93;&lt;/span&gt;.A &amp;lt; sortABList&lt;span class=&quot;error&quot;&gt;&amp;#91;i+1&amp;#93;&lt;/span&gt;.A &lt;/p&gt;
{
    t.Fatalf(&quot;sort error! %d less then %d&quot;, sortABList[i].A, sortABList[i+1].A)

   }
&lt;p&gt;  }&lt;br/&gt;
  fmt.Println()&lt;br/&gt;
 }&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;</description>
                <environment>when use multi field to sort, probably different results</environment>
        <key id="1344119">GODRIVER-1610</key>
            <summary>bson.M can&apos;t be used as a sort document</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</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="-1">Unassigned</assignee>
                                    <reporter username="kaobkaob0812@gmail.com">TheKaobkaob .</reporter>
                        <labels>
                    </labels>
                <created>Fri, 8 May 2020 06:53:24 +0000</created>
                <updated>Fri, 27 Oct 2023 13:16:28 +0000</updated>
                            <resolved>Mon, 11 May 2020 20:05:12 +0000</resolved>
                                    <version>1.3.2</version>
                    <version>1.3.3</version>
                                                    <component>CRUD</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="3077361" author="kaobkaob0812@gmail.com" created="Mon, 11 May 2020 01:38:48 +0000"  >&lt;p&gt;When I change the sorting code, it&apos;s work. Thank you for your help.&lt;/p&gt;

&lt;p&gt;theOption := options.Find().SetSort(bson.D{bson.E&lt;/p&gt;
{Key: &quot;A&quot;, Value: -1}
&lt;p&gt;, bson.E{Key: &quot;B&quot;, Value: -1}})&lt;/p&gt;</comment>
                            <comment id="3076232" author="divjot.arora" created="Fri, 8 May 2020 17:38:20 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=kaobkaob0812%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;kaobkaob0812@gmail.com&quot;&gt;kaobkaob0812@gmail.com&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Thank you for the detailed report and code sample. I think this is happening because the code uses. bson.M for the sort document. The bson.M type wraps a Go map and maps in the language are intentionally do not preserve insertion order when iterating. This means that the actual sort document sent to the server could be marshalled as {b: -1, a: 1} when executing the &lt;tt&gt;Find&lt;/tt&gt; if that&apos;s the order the language gives us when we iterate over the map.&lt;/p&gt;

&lt;p&gt;Can you try using an order-preserving type like bson.D for the &lt;tt&gt;SetSort&lt;/tt&gt; option and see if that fixes the issue?&lt;/p&gt;

&lt;p&gt;&amp;#8211; Divjot&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hx7jaf:</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>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>