<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:55:52 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>[JAVA-1924] When using upsert=true with a custom codec, encodercontext returns isEncodingCollectibleDocument = false</title>
                <link>https://jira.mongodb.org/browse/JAVA-1924</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;when using :&lt;br/&gt;
MongoCollection&amp;lt;SomeEntity&amp;gt; gameCollection = database.getCollection(&quot;entities&quot;, SomeEntity.class);&lt;/p&gt;

&lt;p&gt;gameCollection.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions().upsert(true));&lt;/p&gt;

&lt;p&gt;SomeEntityCodec&apos;s encode method is called with a context that returns encodingCollectibleDocument=false&lt;/p&gt;

&lt;p&gt;See:&lt;br/&gt;
com.mongodb.MongoCollectionImpl : 374&lt;br/&gt;
toBsonDocument(update)&lt;/p&gt;

&lt;p&gt;bson.toBsonDocument(documentClass, codecRegistry)&lt;/p&gt;

&lt;p&gt;neither RawBsonDocument(wrapped, codec), nor BsonDocumentWrapper(wrappedDocument, encoder) takes into account the upsert flag(if it is true, it SHOULD be a encodingCollectibleDocument&lt;/p&gt;
</description>
                <environment></environment>
        <key id="225655">JAVA-1924</key>
            <summary>When using upsert=true with a custom codec, encodercontext returns isEncodingCollectibleDocument = false</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="2">Won&apos;t Fix</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="guicamest">Guillermo Campelo</reporter>
                        <labels>
                            <label>Bug</label>
                    </labels>
                <created>Fri, 14 Aug 2015 04:23:06 +0000</created>
                <updated>Mon, 31 Aug 2015 19:03:16 +0000</updated>
                            <resolved>Thu, 27 Aug 2015 10:51:26 +0000</resolved>
                                    <version>3.0.0</version>
                    <version>3.0.1</version>
                    <version>3.0.2</version>
                    <version>3.0.3</version>
                                                    <component>API</component>
                    <component>BSON</component>
                    <component>Codecs</component>
                                        <votes>0</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="1015800" author="ross@10gen.com" created="Thu, 27 Aug 2015 10:51:26 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=guicamest&quot; class=&quot;user-hover&quot; rel=&quot;guicamest&quot;&gt;guicamest&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just to be clear, any implementation of CollectibleCodec must include the _id field in it&apos;s encode method, independently of it is an insert or an update? If that&apos;s OK, please close this issue as not a bug.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This is definitely is not true and not the case.  Lets look at the &lt;tt&gt;Document&lt;/tt&gt; class.  It has a &lt;tt&gt;DocumentCodec&lt;/tt&gt; that is an implementation of the  &lt;tt&gt;CollectibleCodec&amp;lt;Document&amp;gt;&lt;/tt&gt;, when inserting, we generate the &lt;tt&gt;_id&lt;/tt&gt; and mutate the document - allowing the user to know the &lt;tt&gt;_id&lt;/tt&gt; of the inserted document.  Should &lt;tt&gt;encode&lt;/tt&gt; always include an &lt;tt&gt;_id&lt;/tt&gt;?  No! because &lt;tt&gt;Documents&lt;/tt&gt; are used for all sorts of things, queries, sorts, index definitions etc...  Making &lt;tt&gt;encode&lt;/tt&gt; always include the &lt;tt&gt;_id&lt;/tt&gt; in most circumstances would be wrong.&lt;/p&gt;

&lt;p&gt;Remember as &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=jeff.yemin&quot; class=&quot;user-hover&quot; rel=&quot;jeff.yemin&quot;&gt;jeff.yemin&lt;/a&gt; mentioned previously, &lt;tt&gt;isEncodingCollectibleDocument&lt;/tt&gt; is for the cases when you are representing a BSON Document as it would be stored by MongoDB.  In these scenarios &lt;tt&gt;_id&lt;/tt&gt; if present comes first. That is all that flag is for. &lt;/p&gt;

&lt;h4&gt;&lt;a name=&quot;Should%7B%7BisEncodingCollectibleDocument%3Dtrue%7D%7Dalwaysbethecase%3F&quot;&gt;&lt;/a&gt;Should &lt;tt&gt;isEncodingCollectibleDocument = true&lt;/tt&gt; always be the case?  &lt;/h4&gt;

&lt;p&gt;This is nuanced and depends on the situation. For example take the following compound index: {&lt;tt&gt;price: 1, _id: 1&lt;/tt&gt;}. If &lt;tt&gt;isEncodingCollectibleDocument&lt;/tt&gt; was always true that would make the index mean something totally different.&lt;/p&gt;

&lt;h4&gt;&lt;a name=&quot;Whenshoulda%7B%7BCollectibleCodec%7D%7Dencodea%7B%7Bid%7D%7Dintothedocument%3F&quot;&gt;&lt;/a&gt;When should a &lt;tt&gt;CollectibleCodec&lt;/tt&gt; encode a &lt;tt&gt;_id&lt;/tt&gt; into the document?  &lt;/h4&gt;

&lt;p&gt;In what circumstances do we know that we are actually representing a document as it will be stored in MongoDB?  The answer is only when doing an insert.  So only in those circumstances can we call &lt;tt&gt;generateIdIfAbsentFromDocument&lt;/tt&gt;.  &lt;/p&gt;

&lt;p&gt;(This may or may not add an &lt;tt&gt;_id&lt;/tt&gt;, depending on implementation, if it doesn&apos;t then MongoDB will generate the &lt;tt&gt;_id&lt;/tt&gt;).&lt;/p&gt;

&lt;h4&gt;&lt;a name=&quot;WhataboutUpserts%3F&quot;&gt;&lt;/a&gt;What about Upserts?  &lt;/h4&gt;

&lt;p&gt;Update queries can take multiple keys, in the &lt;tt&gt;Bson&lt;/tt&gt; representation of the update eg : &lt;tt&gt;$set&lt;/tt&gt; &lt;tt&gt;$setOnInsert&lt;/tt&gt;.  Does the &lt;tt&gt;Bson&lt;/tt&gt; type representing an update, represent a &lt;tt&gt;Document&lt;/tt&gt; as it is to be stored by MongoDB?  The answer is no, but the &lt;tt&gt;$set&lt;/tt&gt; part &lt;em&gt;could&lt;/em&gt; represent the document or it could equally be updates to fragments of the &lt;tt&gt;Document&lt;/tt&gt; that is stored in MongoDB.&lt;/p&gt;

&lt;p&gt;&amp;#8211;&lt;/p&gt;

&lt;p&gt;I hope that clarifies the current design and the reasons it acts as such.  As the &lt;tt&gt;Bson&lt;/tt&gt; update provided to an upsert doesn&apos;t necessarily represent a &lt;tt&gt;Document&lt;/tt&gt; as it will be stored, I&apos;m closing this ticket as &quot;Won&apos;t Fix&quot;.   Currently, you will manually have to ensure &lt;tt&gt;_id&lt;/tt&gt; is in either the &lt;tt&gt;query&lt;/tt&gt; or in the &lt;tt&gt;$setOnInsert&lt;/tt&gt; part of the update.&lt;/p&gt;

&lt;p&gt;However, I have added &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-1939&quot; title=&quot;Support automatically setting _id in Upserts.&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-1939&quot;&gt;&lt;del&gt;JAVA-1939&lt;/del&gt;&lt;/a&gt; to track the possibility of injecting a &lt;tt&gt;/{$setOnInsert: {_id: generatedId}/&lt;/tt&gt; into upserts when using &lt;tt&gt;CollectibleCodecs&lt;/tt&gt; which if feasible, could achieve what you are after!&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="1015661" author="guicamest" created="Thu, 27 Aug 2015 04:06:19 +0000"  >&lt;p&gt;Hi Ross, thanks for the depth of the explanation!&lt;/p&gt;

&lt;p&gt;IMHO, the parameter should be renamed, as a CollectibleCodec is directly related to ids and &lt;tt&gt;isEncodingCollectibleDocument&lt;/tt&gt; gives the idea of whether the id should be generated or not. May I suggest the &lt;tt&gt;isInsert&lt;/tt&gt; name?&lt;/p&gt;

&lt;p&gt;Just to be clear, any implementation of CollectibleCodec must include the &lt;b&gt;_id&lt;/b&gt; field in it&apos;s encode method, independently of it is an insert or an update? If that&apos;s OK, please close this issue as not a bug.&lt;/p&gt;

&lt;p&gt;Thanks a lot for the clarification about this issue and keep up with the good work!&lt;/p&gt;</comment>
                            <comment id="1012882" author="ross@10gen.com" created="Mon, 24 Aug 2015 14:05:28 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=guicamest&quot; class=&quot;user-hover&quot; rel=&quot;guicamest&quot;&gt;guicamest&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;I can understand the confusion, however, the driver does not know if any given upsert operation will result in an &lt;b&gt;&lt;tt&gt;update&lt;/tt&gt;&lt;/b&gt; or an &lt;b&gt;&lt;tt&gt;insert&lt;/tt&gt;&lt;/b&gt;.  Only the MongoDB server can tell.  If the operation results in an insert and the &lt;tt&gt;_id&lt;/tt&gt; isn&apos;t supplied, the MongoDB server automatically generates one.  For that reason we can&apos;t automatically generate an &lt;tt&gt;_id&lt;/tt&gt; for &lt;tt&gt;upserts&lt;/tt&gt; that are &lt;tt&gt;inserts&lt;/tt&gt; rather than &lt;tt&gt;updates&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;I hope that clarifies the reasons why &lt;tt&gt;isEncodingCollectibleDocument&lt;/tt&gt; is false for upserts.&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="1009402" author="guicamest" created="Wed, 19 Aug 2015 12:18:41 +0000"  >&lt;p&gt;Hi Jeff, any updates?&lt;/p&gt;</comment>
                            <comment id="1006431" author="guicamest" created="Sat, 15 Aug 2015 05:37:52 +0000"  >&lt;p&gt;It&apos;s kind of confusing, &lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/blob/5c0f4caef43d85a03efa48b99eafa3dead9c668a/bson/src/main/org/bson/codecs/BsonDocumentCodec.java#L118-118&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;CollectibleCodec&lt;/a&gt; states &lt;b&gt;A Codec that generates complete BSON documents for storage in a MongoDB collection&lt;/b&gt; and has methods for checking/generating ids, but as you say, &lt;b&gt;encodingCollectibleDocument&lt;/b&gt; has nothing to do with the &lt;b&gt;_id&lt;/b&gt; field except for the order.&lt;/p&gt;

&lt;p&gt;Besides that, how is a custom &lt;b&gt;CollectibleCodec&lt;/b&gt; supposed to know when to generate an &lt;b&gt;_id&lt;/b&gt; field or not? If it is an &lt;b&gt;upsert=false&lt;/b&gt;, then it shouldn&apos;t generate the &lt;b&gt;_id&lt;/b&gt; field, but if it is an &lt;b&gt;upsert=true&lt;/b&gt; and the document &lt;b&gt;is&lt;/b&gt; to be inserted, it should generate it.&lt;/p&gt;</comment>
                            <comment id="1005958" author="jeff.yemin" created="Fri, 14 Aug 2015 17:33:50 +0000"  >&lt;p&gt;No, that&apos;s not what the flag indicates.  While one could potentially use it for other purposes, It&apos;s currently used in the driver only to encode the fields in such a way that the _id field is placed first in the BSON representation of the document.&lt;/p&gt;</comment>
                            <comment id="1005872" author="guicamest" created="Fri, 14 Aug 2015 16:25:03 +0000"  >&lt;p&gt;Well, the flag indicates whether or not include the &quot;_id&quot; field in the serialization, correct? If it is an upsert=true, it might be an insert, in which case the _id should be serialized.&lt;/p&gt;</comment>
                            <comment id="1005609" author="jeff.yemin" created="Fri, 14 Aug 2015 13:12:07 +0000"  >&lt;p&gt;Yes, that&apos;s the expected behavior, and it is not governed at all by the upsert flag.  The encodingCollectibleDocument property will be true only when encoding a document that is going to be inserted in a collection, but never for filters or updates (which use $ operators like $set).  You can see it&apos;s intended usage &lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/blob/5c0f4caef43d85a03efa48b99eafa3dead9c668a/bson/src/main/org/bson/codecs/BsonDocumentCodec.java#L118-118&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;here&lt;/a&gt;, for example.&lt;/p&gt;

&lt;p&gt;What are you trying to use the flag for? &lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10038"><![CDATA[Fully Compatible]]></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|hscjcv:</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>