<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:53:06 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-798] DeepCopy ObjectId causes an indefinite Insert() instead of Upsert() in the Save() method.</title>
                <link>https://jira.mongodb.org/browse/JAVA-798</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Hello&lt;/p&gt;

&lt;p&gt;I use a DeepCopy library (&lt;a href=&quot;https://code.google.com/p/cloning/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://code.google.com/p/cloning/&lt;/a&gt;) in my application to speed-up tremendous amount of pre-constructed objects with a deep hierarchy. However, the ObjectId class really doesn&apos;t react well to this behavior.&lt;/p&gt;

&lt;p&gt;If I create a &quot;new ObjectId()&quot;, the flag &quot;_new&quot; is set to &quot;true&quot;. If I store, let&apos;s say in a HashMap, my Object containing this Id and I try to SAVE() my object it works for the first time. However, the second time I SAVE() my object using your driver, MongoDB tells me that there&apos;s a violation of the Index.&lt;/p&gt;

&lt;p&gt;Looking at your source code :&lt;/p&gt;

&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Saves an object to this collection (does insert or update based on the object _id).&lt;/li&gt;
	&lt;li&gt;@param jo the &amp;lt;code&amp;gt;DBObject&amp;lt;/code&amp;gt; to save&lt;/li&gt;
	&lt;li&gt;@param concern the write concern&lt;/li&gt;
	&lt;li&gt;@return&lt;/li&gt;
	&lt;li&gt;@throws MongoException&lt;br/&gt;
     */&lt;br/&gt;
    public WriteResult save( DBObject jo, WriteConcern concern ){&lt;br/&gt;
        if ( checkReadOnly( true ) )&lt;br/&gt;
            return null;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;        _checkObject( jo , false , false );&lt;/p&gt;

&lt;p&gt;        Object id = jo.get( &quot;_id&quot; );&lt;/p&gt;

&lt;p&gt;        if ( id == null || ( id instanceof ObjectId &amp;amp;&amp;amp; ((ObjectId)id).isNew() ) )&lt;/p&gt;
{
            if ( id != null &amp;amp;&amp;amp; id instanceof ObjectId )
                ((ObjectId)id).notNew();
            if ( concern == null )
            	return insert( jo );
            else
            	return insert( jo, concern );
        }

&lt;p&gt;        DBObject q = new BasicDBObject();&lt;br/&gt;
        q.put( &quot;_id&quot; , id );&lt;br/&gt;
        if ( concern == null )&lt;br/&gt;
        	return update( q , jo , true , false );&lt;br/&gt;
        else&lt;br/&gt;
        	return update( q , jo , true , false , concern );&lt;/p&gt;

&lt;p&gt;    }&lt;/p&gt;

&lt;p&gt;It Looks like since the ObjectId is DeepCopied, the Driver always do an &quot;Insert&quot; instead of an Update : Upsert = True. Shouldn&apos;t the &quot;save&quot; method always process to a Upsert instead of a Insert / Update?&lt;/p&gt;

&lt;p&gt;I managed a workaround setting my ObjectId to notNew() everytime I create one and &quot;everything&quot; seems to work fine now, the driver doing an Update (Upsert:true).&lt;/p&gt;

&lt;p&gt;My Questions : Why do you Insert if the Upsert command works fine? Is there any performance Issue I should be aware of using this &quot;trick&quot;? Will MongoDB handle Upsert poorly compared to &quot;real&quot; inserts?&lt;/p&gt;</description>
                <environment></environment>
        <key id="70154">JAVA-798</key>
            <summary>DeepCopy ObjectId causes an indefinite Insert() instead of Upsert() in the Save() method.</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="2" iconUrl="https://jira.mongodb.org/images/icons/priorities/critical.svg">Critical - P2</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="dmarcotte">Daniel Marcotte</reporter>
                        <labels>
                            <label>DeepCopy</label>
                            <label>ObjectId</label>
                            <label>Save</label>
                            <label>Upsert</label>
                    </labels>
                <created>Fri, 29 Mar 2013 13:42:59 +0000</created>
                <updated>Tue, 31 Mar 2015 20:09:59 +0000</updated>
                            <resolved>Tue, 25 Jun 2013 17:02:23 +0000</resolved>
                                    <version>2.9.2</version>
                    <version>2.9.3</version>
                                    <fixVersion>3.0.0</fixVersion>
                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="869490" author="jeff.yemin" created="Tue, 31 Mar 2015 20:09:59 +0000"  >&lt;p&gt;Closing all resolved 3.0.0 issues, as 3.0.0 has been tagged and released.&lt;/p&gt;</comment>
                            <comment id="367441" author="jeff.yemin" created="Tue, 25 Jun 2013 17:05:25 +0000"  >&lt;p&gt;Fixed in &lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/aa4b4355c808e045ce00cc1dfbdd0bf78fb6901a&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/aa4b4355c808e045ce00cc1dfbdd0bf78fb6901a&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="301979" author="jeff.yemin" created="Sat, 30 Mar 2013 00:21:49 +0000"  >&lt;p&gt;I&apos;ll have to check, but I would think that it would be minimal.&lt;/p&gt;</comment>
                            <comment id="301867" author="dmarcotte" created="Fri, 29 Mar 2013 21:27:51 +0000"  >&lt;p&gt;Thanks, a Stateless ObjectId will definitely help for Serialization/Deserialization purpose other than with the MongoDB Java Driver.&lt;/p&gt;

&lt;p&gt;Is there any performance cost within MongoDB for Insert vs Update (Upsert:true)?&lt;/p&gt;</comment>
                            <comment id="301683" author="jeff.yemin" created="Fri, 29 Mar 2013 17:22:20 +0000"  >&lt;p&gt;I&apos;m not a fan of isNew() either.  I plan to remove it in 3.0 and make ObjectId immutable.&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|hrmzvz:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>47908</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>