<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:52:29 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-517] DuplicateKey exception not properly thrown through findAndModify</title>
                <link>https://jira.mongodb.org/browse/JAVA-517</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;when I try to insert a duplicate through this code:&lt;/p&gt;

&lt;p&gt;try &lt;/p&gt;
{
 
coll.findAndModify( queryId( document ).and( &quot;p.revision&quot; ).is( document.getRevision() ).get(),
                    new BasicDBObject( &quot;$set&quot;, mods ) );
            }
&lt;p&gt;}&lt;br/&gt;
catch ( MongoException e )&lt;br/&gt;
{&lt;/p&gt;

&lt;p&gt;I&apos;m not able to directly catch the DuplicateKey Exception, as findAndModify will not throw such exception. Rather it throws an CommandResult$CommandFailure, sadly this Exception isn&apos;t public so I can&apos;t catch it nor can I cast to it from MongoException.&lt;br/&gt;
Catching a MongoException.DuplicateKey isn&apos;t working either as this exception isn&apos;t thrown at all. The only usage of DuplicateKey is within CommandResult.getException, but as CommandFailure isn&apos;t a subtype of CommandResult I neither can&apos;t cast to this, so the only way to currently find out what exception was thrown is to dig into the MongoException payload which looks like:&lt;/p&gt;

&lt;p&gt;com.mongodb.CommandResult$CommandFailure: command failed [command&lt;br/&gt;
failed &lt;span class=&quot;error&quot;&gt;&amp;#91;findandmodify&amp;#93;&lt;/span&gt; { &quot;serverUsed&quot; : &quot;pheinze-0830:27017&quot; ,&lt;br/&gt;
&quot;lastErrorObject&quot; : { &quot;err&quot; : &quot;E11000 duplicate key error index:&lt;br/&gt;
test.address.$ns_1_t_1_p.doc_id_1  dup key: { : \&quot;_&lt;em&gt;nn&lt;/em&gt;_\&quot;, : \&quot;N\&quot;, :&lt;br/&gt;
\&quot;testPutPKTwice\&quot; }&quot; , &quot;code&quot; : 11000 , &quot;n&quot; : 0 , &quot;connectionId&quot; :&lt;br/&gt;
44 , &quot;ok&quot; : 1.0} , &quot;errmsg&quot; : &quot;E11000 duplicate key error index:&lt;br/&gt;
test.address.$ns_1_t_1_p.doc_id_1  dup key: { : \&quot;_&lt;em&gt;nn&lt;/em&gt;_\&quot;, : \&quot;N\&quot;, :&lt;br/&gt;
\&quot;testPutPKTwice\&quot; }&quot; , &quot;ok&quot; : 0.0}&lt;/p&gt;

&lt;p&gt;to check getLastError or to manually start another query to check if the key is already used. Neither of them feels to me that this is the way it&apos;s meant to be.&lt;/p&gt;</description>
                <environment>Java 6, Fedora 16</environment>
        <key id="30115">JAVA-517</key>
            <summary>DuplicateKey exception not properly thrown through findAndModify</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="4" iconUrl="https://jira.mongodb.org/images/icons/priorities/minor.svg">Minor - P4</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="3">Duplicate</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="pheinze">Philipp Heinze</reporter>
                        <labels>
                            <label>DuplicateKey</label>
                            <label>MongoException</label>
                            <label>findAndModify</label>
                    </labels>
                <created>Tue, 7 Feb 2012 19:20:10 +0000</created>
                <updated>Wed, 28 Oct 2015 17:59:43 +0000</updated>
                            <resolved>Wed, 28 Oct 2015 17:59:43 +0000</resolved>
                                    <version>2.7.3</version>
                                                    <component>API</component>
                                        <votes>1</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="1073122" author="jeff.yemin" created="Wed, 28 Oct 2015 17:59:43 +0000"  >&lt;p&gt;Closing as duplicate of &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-1821&quot; title=&quot;findAndModify() with upsert throws wrong exception for duplicate key errors&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-1821&quot;&gt;&lt;del&gt;JAVA-1821&lt;/del&gt;&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="277283" author="jeff.yemin" created="Wed, 27 Feb 2013 17:09:09 +0000"  >&lt;p&gt;Note that in 2.11.0 there will be a new public class CommandFailureException, from which you can get the CommandResult.&lt;/p&gt;</comment>
                            <comment id="86233" author="jeff.yemin" created="Tue, 7 Feb 2012 20:49:18 +0000"  >&lt;p&gt;For findAndModify command, the 11001 error is wrapped in the lastErrorObject field of the server response, and it&apos;s being ignored.  The main response is !ok(), which causes com.mongodb.CommandResult#getException to throw CommandFailure exception.&lt;/p&gt;

&lt;p&gt;Solution may be to drill in to lastErrorObject field to look for the 11001 error code.&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                            <outwardlinks description="duplicates">
                                        <issuelink>
            <issuekey id="205313">JAVA-1821</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="64925">JAVA-757</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <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|hrga7b:</customfieldvalue>

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