<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:37:54 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-2137] empty/nil slices create null field, which can&apos;t be used by various functions</title>
                <link>https://jira.mongodb.org/browse/GODRIVER-2137</link>
                <project id="14289" key="GODRIVER">Go Driver</project>
                    <description>&lt;p&gt;As similarly expressed in&#160;&lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-971&quot; title=&quot;ability to marshal empty slices as bson [] rather than nil&quot; class=&quot;issue-link&quot; data-issue-key=&quot;GODRIVER-971&quot;&gt;&lt;del&gt;GODRIVER-971&lt;/del&gt;&lt;/a&gt; (and added support for via mgocompat in &lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-1362&quot; title=&quot;Add SliceCodec for mgocompat&quot; class=&quot;issue-link&quot; data-issue-key=&quot;GODRIVER-1362&quot;&gt;&lt;del&gt;GODRIVER-1362&lt;/del&gt;&lt;/a&gt;), the default behavior of the default registry used by the Go library is that an empty array (initialized but no elements) or nil array (uninitialized) becomes null when encoded to BSON, and stored in Mongo.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;The primary issue with this, is that things like $push, $in, $addToSet, etc do not work as expected when the value is set to null.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;The two current solutions are:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Use mgocompat registry &#8211; a feature that&apos;s not very obvious, and it further strays away from the &quot;recommended&quot; approach to using the library. This however works as expected, writing an empty array into mongo.&lt;/li&gt;
	&lt;li&gt;Use &quot;omitempty&quot; on the field, which doesn&apos;t write the field at all
	&lt;ol&gt;
		&lt;li&gt;Most methods (e.g. $push, $addToSet, etc) work when the field doesn&apos;t exist, and those methods will create the field.&lt;/li&gt;
		&lt;li&gt;It&apos;s not obvious that this would be needed to prevent the issue, so users would likely deploy and start writing data before realizing that this may not do what they expect. One would have to first do a database migration to remove all fields that are supposed to be arrays (which were written as null, as this isn&apos;t what they were expecting), and then add the necessary tag field for future writes.&lt;/li&gt;
		&lt;li&gt;There are still some edge-cases with this approach I think in terms of validation (i.e. a field that is required, but can be empty, now can&apos;t be required in schema, among others), which would have to be written around in code.&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;Given the field being written as an empty array via bson, can be converted back to nil on the Go side, I don&apos;t believe there is a downside in taking the mgocompat approach, and bringing this in as the default. This would also ensure standard methods are prone to work as expected, play better with folks who are using validation, and seems more obvious.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;At a minimum, I wish this would be re-visited, given the nature of how Go arrays have a nil zero value, which isn&apos;t necessarily common with some of the other languages which are supported, and thus I think may warrant an exception to the standard &quot;take exactly what we&apos;re given&quot; bson encoding semantics.&lt;/p&gt;</description>
                <environment></environment>
        <key id="1857637">GODRIVER-2137</key>
            <summary>empty/nil slices create null field, which can&apos;t be used by various functions</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="4" iconUrl="https://jira.mongodb.org/images/icons/priorities/minor.svg">Minor - P4</priority>
                        <status id="10038" iconUrl="https://jira.mongodb.org/images/icons/subtask.gif" description="">Backlog</status>
                    <statusCategory id="2" key="new" colorName="default"/>
                                    <resolution id="-1">Unresolved</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="me@liamstanley.io">Liam Stanley</reporter>
                        <labels>
                    </labels>
                <created>Sun, 22 Aug 2021 04:21:22 +0000</created>
                <updated>Wed, 26 Jul 2023 21:48:54 +0000</updated>
                                                                            <component>BSON</component>
                                        <votes>0</votes>
                                    <watches>5</watches>
                                                                                                                <comments>
                            <comment id="5242889" author="JIRAUSER1259527" created="Thu, 2 Mar 2023 07:30:00 +0000"  >&lt;p&gt;Here&apos;s what we&apos;re currently planning to do for various use cases.&lt;/p&gt;

&lt;h2&gt;&lt;a name=&quot;GostructsasMongoDBdocuments&quot;&gt;&lt;/a&gt;Go structs as MongoDB documents&lt;/h2&gt;
&lt;p&gt;To support the use case of encoding Go structs as MongoDB documents, we will add an &lt;tt&gt;omitnil&lt;/tt&gt; BSON struct tag that only omits &lt;tt&gt;nil&lt;/tt&gt; values but includes empty values. The &lt;tt&gt;$push&lt;/tt&gt; and &lt;tt&gt;$addToSet&lt;/tt&gt; update operators work the same when modifying document fields that are empty arrays and document fields that do not exist (the only problem is document fields that are BSON null), so the behavior of &lt;tt&gt;omitnil&lt;/tt&gt; maps well to that use case. Additionally, other MongoDB drivers (e.g. Java and Rust) offer a similar behaviors. See &lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-2765&quot; title=&quot;Add an &amp;quot;omitnil&amp;quot; BSON struct tag&quot; class=&quot;issue-link&quot; data-issue-key=&quot;GODRIVER-2765&quot;&gt;GODRIVER-2765&lt;/a&gt; for more details.&lt;/p&gt;

&lt;h2&gt;&lt;a name=&quot;MakeconfiguringtheMongoDB%7B%7BClient%7D%7DBSONmarshalingbehaviormucheasier&quot;&gt;&lt;/a&gt;Make configuring the MongoDB &lt;tt&gt;Client&lt;/tt&gt; BSON marshaling behavior much easier&lt;/h2&gt;
&lt;p&gt;To support the use case of changing Client-wide BSON marshaling behavior, we will move all of the current configuration options that are buried in the &lt;tt&gt;bsoncodec&lt;/tt&gt; package onto the &lt;tt&gt;bson.Encoder&lt;/tt&gt; and &lt;tt&gt;bson.Decoder&lt;/tt&gt; types. For example, instead of having to create a &lt;tt&gt;SliceCodec&lt;/tt&gt; and set &lt;tt&gt;EncodeNilAsEmpty&lt;/tt&gt;, you only have to create a &lt;tt&gt;bson.Encoder&lt;/tt&gt; and then call &lt;tt&gt;SetNilAsEmpty&lt;/tt&gt;. See &lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-2716&quot; title=&quot;Deprecate all &amp;quot;bsoncodec.*Codec&amp;quot; types&quot; class=&quot;issue-link&quot; data-issue-key=&quot;GODRIVER-2716&quot;&gt;&lt;del&gt;GODRIVER-2716&lt;/del&gt;&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=me%40liamstanley.io&quot; class=&quot;user-hover&quot; rel=&quot;me@liamstanley.io&quot;&gt;me@liamstanley.io&lt;/a&gt; Does that sound like it will resolve some of your original concerns?&lt;/p&gt;</comment>
                            <comment id="5026736" author="benji.rewis" created="Fri, 2 Dec 2022 22:53:15 +0000"  >&lt;p&gt;&lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=karthickcseapitam%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;karthickcseapitam@gmail.com&quot;&gt;karthickcseapitam@gmail.com&lt;/a&gt;&#160;I&apos;ve responded to your community &lt;a href=&quot;https://www.mongodb.com/community/forums/t/how-to-use-mongocompact-package-in-mongoclient-connect-options/202495/2&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;forum post&lt;/a&gt;; that&apos;s probably the best place to continue this conversation. Thanks!&lt;/p&gt;</comment>
                            <comment id="5024711" author="JIRAUSER1271612" created="Fri, 2 Dec 2022 10:31:12 +0000"  >&lt;p&gt;&lt;span class=&quot;nobr&quot;&gt;&lt;a href=&quot;mailto:benji.rewis@mongodb.com&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;benji.rewis@mongodb.com&lt;sup&gt;&lt;img class=&quot;rendericon&quot; src=&quot;https://jira.mongodb.org/images/icons/mail_small.gif&quot; height=&quot;12&quot; width=&quot;13&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/sup&gt;&lt;/a&gt;&lt;/span&gt;&#160; , How can I use mgocompact with my client options ? currently my code looks like below , I don&apos;t see the &lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/blob/v1.11.0/bson/mgocompat/registry.go#L39&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;mongocompact&lt;/a&gt; registry file in my vendor directory not sure why ,&#160;&lt;/p&gt;
&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;tM := reflect.TypeOf(bson.M{})&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;registry := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build()&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;clientOpts := options.Client().ApplyURI(URI).SetAuth(info).SetRegistry(registry) client, err := mongo.Connect(ctx, clientOpts)&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;&#160;&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;
&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;how can I add also NewRespectNilValuesRegistryBuilder().Build() in the above code ,?&#160;&lt;/p&gt;

&lt;p&gt;Also us emptyarray BSON tag implemented by any chance ?&#160;&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;</comment>
                            <comment id="4092573" author="benji.rewis" created="Wed, 29 Sep 2021 18:18:46 +0000"  >&lt;p&gt;All good, &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=me%40liamstanley.io&quot; class=&quot;user-hover&quot; rel=&quot;me@liamstanley.io&quot;&gt;me@liamstanley.io&lt;/a&gt;! I&apos;ll reopen this ticket for now and mark it with fix version 2.0. We can reconsider changing our default behavior at that point in time. For now, it seems like using the &lt;tt&gt;mgocompat&lt;/tt&gt; registry is a viable workaround if you want both empty-initialized and nil arrays to marshal and unmarshal to and from empty BSON arrays.&lt;/p&gt;

&lt;p&gt;I&apos;ll bring up the idea of a new &lt;tt&gt;emptyarray&lt;/tt&gt;&#160;BSON tag with the rest of the team, too.&lt;/p&gt;</comment>
                            <comment id="4088095" author="JIRAUSER1258130" created="Mon, 27 Sep 2021 23:26:34 +0000"  >&lt;p&gt;I&apos;m sorry for the delay. You are correct, I thought I tested that specific scenario previously, but I guess I didn&apos;t test properly.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;I guess that does change things a bit, though I do think the default for nil arrays being changed should be something that is discussed. I&apos;m not confident in if it should be the standard, but it does seem to make sense to me.&lt;/p&gt;

&lt;p&gt;I wonder if a tag value (similar to omitempty), would be better (not as much of a potential breaking change), that when present, tells bson that it:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Should output an empty initialized array when encoding&lt;/li&gt;
	&lt;li&gt;Should return an empty initialized array when decoding, if the db has nil rather than an empty array.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Something like `bson:&quot;,emptyarray&quot;` or similar, on the field.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;I still think the default changing makes sense for most users, since nil is harder to work with (e.g. $addToSet and $push fail, so you&apos;d have to convert first, then use those methods). If we were to change the default, we could use tag fields to keep the original behavior if for some reason someone wanted to continue to write nil&apos;s.&lt;/p&gt;</comment>
                            <comment id="4062231" author="backlog-server-pm" created="Wed, 15 Sep 2021 12:00:00 +0000"  >&lt;p&gt;There hasn&apos;t been any recent activity on this ticket, so we&apos;re resolving it. Thanks for reaching out! Please feel free to comment on this if you&apos;re able to provide more information.&lt;/p&gt;</comment>
                            <comment id="4032044" author="benji.rewis" created="Tue, 31 Aug 2021 17:39:39 +0000"  >&lt;p&gt;Thanks again for your improvement suggestion &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=me%40liamstanley.io&quot; class=&quot;user-hover&quot; rel=&quot;me@liamstanley.io&quot;&gt;me@liamstanley.io&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;If I&#8217;m not mistaken, Go arrays that are empty but initialized will &lt;tt&gt;Marshal&lt;/tt&gt; to an empty BSON array. Go arrays that are nil will become null when encoded to BSON. Please see the &lt;tt&gt;emptyarraymarshal_test.go&lt;/tt&gt; file I&#8217;ve attached. Were you referring to a different form of &#8220;empty&#8221; slice?&lt;/p&gt;

&lt;p&gt;In any case, modifying the default behavior of the default registry used by the Go BSON library in order to encode nil Go arrays as empty BSON arrays would be a backwards-breaking API change. Users are likely relying on the existing behavior.&lt;/p&gt;

&lt;p&gt;I do see an argument for encoding nil Go arrays to empty BSON arrays, but that change would have to be done in a major release (like the upcoming Go driver 2.0). I also agree that neither the &lt;tt&gt;mgocompat&lt;/tt&gt; registry nor the &lt;tt&gt;omitempty&lt;/tt&gt; struct tag are immediately obvious solutions for this use-case. So, we could certainly Open this ticket for consideration in 2.0.&lt;/p&gt;</comment>
                            <comment id="4030058" author="benji.rewis" created="Mon, 30 Aug 2021 20:08:33 +0000"  >&lt;p&gt;Hello &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=me%40liamstanley.io&quot; class=&quot;user-hover&quot; rel=&quot;me@liamstanley.io&quot;&gt;me@liamstanley.io&lt;/a&gt;&#160;and apologies for the delayed response. Thanks for your improvement suggestions; we&apos;re investigating now!&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="2279237">GODRIVER-2765</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="2048134">GODRIVER-2425</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="2401618">GODRIVER-2920</issuekey>
        </issuelink>
                            </outwardlinks>
                                                                <inwardlinks description="is related to">
                                        <issuelink>
            <issuekey id="740614">GODRIVER-971</issuekey>
        </issuelink>
            <issuelink>
            <issuekey id="973174">GODRIVER-1362</issuekey>
        </issuelink>
                            </inwardlinks>
                                    </issuelinktype>
                    </issuelinks>
                <attachments>
                            <attachment id="332799" name="emptyarraymarshal_test.go" size="1315" author="benji.rewis@mongodb.com" created="Tue, 31 Aug 2021 17:39:47 +0000"/>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10012"><![CDATA[Major Change]]></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_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|htywjw:</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>