<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:59:38 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>[CXX-581] insertion with duplicate _id did not fail, it updated the document  instead.</title>
                <link>https://jira.mongodb.org/browse/CXX-581</link>
                <project id="11980" key="CXX">C++ Driver</project>
                    <description>&lt;p&gt;  using the following sample program, if I ran it, modify eventString values and compile and run again, both runs will succeed with data updated. I am expecting the second run to fail with &quot;duplicate key error&quot; .&lt;/p&gt;

&lt;p&gt;  1 #include &quot;mongo/client/dbclient.h&quot;&lt;br/&gt;
  2 #include &amp;lt;iostream&amp;gt;&lt;br/&gt;
  3 #include &amp;lt;vector&amp;gt;&lt;br/&gt;
  4 #include &amp;lt;sys/time.h&amp;gt;&lt;br/&gt;
  5 &lt;br/&gt;
  6 //#define RECORDS 5000000&lt;br/&gt;
  7 //#define RECORDS 800&lt;br/&gt;
  8 #define RECORDS 2&lt;br/&gt;
  9 #define BULKSIZE 10000&lt;br/&gt;
 10 const char *eventString = &quot;abcdefg&quot;;&lt;br/&gt;
 11 int main(int argc, char **argv) {&lt;br/&gt;
 12     if (!mongo::client::initialize().isOK()) &lt;/p&gt;
{
 13         std::cout &amp;lt;&amp;lt; &quot;mongo initialize failed&quot; &amp;lt;&amp;lt; std::endl;
 14     }
&lt;p&gt; 15     std::vector&amp;lt;mongo::BSONObj&amp;gt; bulk_data;&lt;br/&gt;
 16     mongo::DBClientConnection mongo;&lt;br/&gt;
 17     mongo::HostAndPort mongoHostAndPort(&quot;localhost:27017&quot;);&lt;br/&gt;
 18     mongo::ConnectionString mongoConnectionString(mongoHostAndPort);&lt;br/&gt;
 19     //mongo.connect(&quot;localhost&quot;);&lt;br/&gt;
 20     std::string errString; //NOLINT&lt;br/&gt;
 21     if (!mongo.connect(mongoHostAndPort, errString)) &lt;/p&gt;
{
 22         std::cout &amp;lt;&amp;lt; &quot;fail to connect mongoDB: &quot; &amp;lt;&amp;lt; errString;
 23         return 1;
 24     }
&lt;p&gt;;&lt;br/&gt;
 25     mongo.dropCollection(&quot;insert_test.col1&quot;);&lt;br/&gt;
 26 &lt;br/&gt;
 27     struct timeval start;&lt;br/&gt;
 28     gettimeofday(&amp;amp;start, NULL);&lt;br/&gt;
 29 &lt;br/&gt;
 30     uint64_t id = 1;&lt;br/&gt;
 31     int count = 0;&lt;br/&gt;
 32     int mongodbFsync = 0;&lt;br/&gt;
 33     int mongodbJournal = 0;&lt;br/&gt;
 34     int mongodbWriteconcern = 1;&lt;br/&gt;
 35     int mongodbWtimeoutInMilliseconds = 1;&lt;br/&gt;
 36     mongo.createIndex(&quot;insert_test.collection1&quot;, BSON(&quot;EventId&quot; &amp;lt;&amp;lt; 1));&lt;br/&gt;
 37     mongo.createIndex(&quot;insert_test.collection1&quot;, BSON(&quot;WalletId&quot; &amp;lt;&amp;lt; 1));&lt;br/&gt;
 38     mongo.createIndex(&quot;insert_test.collection1&quot;, BSON(&quot;DeviceId&quot; &amp;lt;&amp;lt; 1));&lt;br/&gt;
 39   try {&lt;br/&gt;
 40     for (int i=1; i&amp;lt;=RECORDS; i++) {&lt;br/&gt;
 41         count++;&lt;br/&gt;
 42         //++id;&lt;br/&gt;
 43         mongo::BSONObj record = BSON (&quot;_id&quot; &amp;lt;&amp;lt; static_cast&amp;lt;long long int&amp;gt;(id)&lt;br/&gt;
 44         &amp;lt;&amp;lt; &quot;EventId&quot; &amp;lt;&amp;lt; 2&lt;br/&gt;
 45         &amp;lt;&amp;lt; &quot;WalletId&quot; &amp;lt;&amp;lt; 3&lt;br/&gt;
 46         &amp;lt;&amp;lt; &quot;DeviceId&quot; &amp;lt;&amp;lt; 4&lt;br/&gt;
 47         &amp;lt;&amp;lt; &quot;mystring&quot; &amp;lt;&amp;lt; eventString);&lt;br/&gt;
 48 &lt;br/&gt;
 49         bulk_data.push_back(record);&lt;br/&gt;
 50 &lt;br/&gt;
 51        if (i % BULKSIZE == 0) {&lt;br/&gt;
 52             mongo.insert(&quot;insert_test.col1&quot;, bulk_data);&lt;br/&gt;
 53             std::string e = mongo.getLastError();&lt;br/&gt;
 54             if (!e.empty()) &lt;/p&gt;
{
 55                 std::cout &amp;lt;&amp;lt; &quot;insert failed for: &quot; &amp;lt;&amp;lt; e &amp;lt;&amp;lt; std::endl;
 56             }
&lt;p&gt; 57             bulk_data.clear();&lt;br/&gt;
 58             count = 0;&lt;br/&gt;
 59 &lt;br/&gt;
 60         }&lt;br/&gt;
 61 &lt;br/&gt;
 62     }&lt;br/&gt;
 63     if (count != 0) {&lt;br/&gt;
 64         mongo.insert(&quot;insert_test.col1&quot;, bulk_data);&lt;br/&gt;
 65         std::string e = mongo.getLastError();&lt;br/&gt;
 66         if (!e.empty()) &lt;/p&gt;
{
 67             std::cout &amp;lt;&amp;lt; &quot;insert failed for: &quot; &amp;lt;&amp;lt; e &amp;lt;&amp;lt; std::endl;
 68         }
&lt;p&gt; 69         bulk_data.clear();&lt;br/&gt;
 70         count = 0;&lt;br/&gt;
 71     }&lt;br/&gt;
 71     }&lt;br/&gt;
 72   } catch (const mongo::DBException&amp;amp;   exc) &lt;/p&gt;
{
 73     std::cout &amp;lt;&amp;lt; &quot;Caught an exception during getLastError.\n&quot; &amp;lt;&amp;lt; &quot;Exception&apos;s what()=&quot; &amp;lt;&amp;lt; exc.what    () &amp;lt;&amp;lt; std::endl;
 74   }
&lt;p&gt; catch (...) &lt;/p&gt;
{
 75     std::cout &amp;lt;&amp;lt; &quot;Caught an unknown exception during getLastError.&quot; &amp;lt;&amp;lt; std::endl;
 76   }
&lt;p&gt; 77     struct timeval end;&lt;br/&gt;
 78     gettimeofday(&amp;amp;end, NULL);&lt;br/&gt;
 79     int now = (end.tv_sec * 1000) + (int)(end.tv_usec/1000);&lt;br/&gt;
 80     int elapsed_time = now - ((start.tv_sec * 1000) +&lt;br/&gt;
 81         (int)(start.tv_usec/1000));&lt;br/&gt;
 82 &lt;br/&gt;
 83     std::cout &amp;lt;&amp;lt; &quot;rate: &quot; &amp;lt;&amp;lt; RECORDS/(elapsed_time) &amp;lt;&amp;lt; &quot;/msec&quot; &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
 84 &lt;br/&gt;
 85     std::cout &amp;lt;&amp;lt; &quot;count:&quot; &amp;lt;&amp;lt; mongo.count(&quot;insert_test.col1&quot;) &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
 86 &lt;br/&gt;
 87     std::auto_ptr&amp;lt;mongo::DBClientCursor&amp;gt; cursor = mongo.query(&quot;insert_test.col1&quot;, mongo::BSONObj()    );&lt;br/&gt;
 88     if (!cursor.get()) &lt;/p&gt;
{
 89         std::cout &amp;lt;&amp;lt; &quot;query failure&quot; &amp;lt;&amp;lt; std::endl;
 90         return EXIT_FAILURE;
 91     }
&lt;p&gt; 92 &lt;br/&gt;
 93     int loopCount = 0;&lt;br/&gt;
 94     while( cursor-&amp;gt;more() ) {&lt;br/&gt;
 95         std::cout &amp;lt;&amp;lt; cursor-&amp;gt;next().toString() &amp;lt;&amp;lt; std::endl;&lt;br/&gt;
 96         ++loopCount;&lt;br/&gt;
 97         if (loopCount == 100) &lt;/p&gt;
{
 98             break;
 99         }
&lt;p&gt;100     }&lt;br/&gt;
101     mongo::client::shutdown();&lt;br/&gt;
102     return 0;&lt;br/&gt;
103 }&lt;/p&gt;</description>
                <environment></environment>
        <key id="196302">CXX-581</key>
            <summary>insertion with duplicate _id did not fail, it updated the document  instead.</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="9">Done</resolution>
                                        <assignee username="adam.midvidy">Adam Midvidy</assignee>
                                    <reporter username="Judy.Han">Judy Han [X]</reporter>
                        <labels>
                    </labels>
                <created>Wed, 15 Apr 2015 21:21:49 +0000</created>
                <updated>Wed, 15 Apr 2015 22:01:50 +0000</updated>
                            <resolved>Wed, 15 Apr 2015 22:01:50 +0000</resolved>
                                    <version>legacy-1.0.0</version>
                                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="882290" author="judy.han" created="Wed, 15 Apr 2015 21:59:41 +0000"  >&lt;p&gt;Hi Adam,&lt;br/&gt;
Thanks for the quick response and the explanation.&lt;br/&gt;
Here is the documentation I was referring to:&lt;br/&gt;
&lt;a href=&quot;http://api.mongodb.org/cxx/current/classmongo_1_1_d_b_client_with_commands.html#aa17adcfed028b8dfadcfd19fcf7081aa&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://api.mongodb.org/cxx/current/classmongo_1_1_d_b_client_with_commands.html#aa17adcfed028b8dfadcfd19fcf7081aa&lt;/a&gt;&lt;br/&gt;
it mentions that:&lt;br/&gt;
Returns (of type string):&lt;br/&gt;
    error message text, or empty string if no error. &lt;br/&gt;
Feel free to close the ticke now. &lt;img class=&quot;emoticon&quot; src=&quot;https://jira.mongodb.org/images/icons/emoticons/smile.png&quot; height=&quot;16&quot; width=&quot;16&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt; &lt;br/&gt;
Thanks!&lt;br/&gt;
Judy&lt;/p&gt;</comment>
                            <comment id="882281" author="adam.midvidy" created="Wed, 15 Apr 2015 21:49:58 +0000"  >&lt;p&gt;Hi Judy,&lt;/p&gt;

&lt;p&gt;Yes it is expected behavior that OperationException would be thrown for a duplicate key error. The legacy driver does not have a separate exception type for duplicate key error, although some other drivers do. This is something that is unlikely to change given our desire to keep legacy driver changes limited to bug fixes.&lt;/p&gt;

&lt;p&gt;Can you show me which documentation you believe this behavior contradicts?&lt;/p&gt;

&lt;p&gt;And yes, getLastError is thread safe if each thread has its own connection. The server stores getLastError state on a per-connection basis.&lt;/p&gt;</comment>
                            <comment id="882270" author="judy.han" created="Wed, 15 Apr 2015 21:44:48 +0000"  >&lt;p&gt;sorry, I said &quot;close&quot; too quickly. Could you reopen it?&lt;/p&gt;

&lt;p&gt;after removing line 25, for second run, I am expecting something like:&lt;br/&gt;
insert failed for: duplicate key error.&lt;/p&gt;

&lt;p&gt;instead I got:&lt;br/&gt;
Caught an exception during getLastError.&lt;br/&gt;
Exception&apos;s what()=OperationException: { index: 0, code: 11000, errmsg: &quot;E11000 duplicate key error dup key: { : 1 }&quot;, op: &lt;/p&gt;
{ _id: 1, EventId: 2, WalletId: 3, DeviceId: 4, mystring: &quot;abcde&quot; }
&lt;p&gt; }&lt;/p&gt;

&lt;p&gt;Is that expected? It does not match the documenation.&lt;/p&gt;

&lt;p&gt;Another question I have is:&lt;br/&gt;
is getLastError() thread safe? Assuming each thread has its own connection.&lt;/p&gt;

&lt;p&gt;Thanks!&lt;br/&gt;
Judy&lt;/p&gt;</comment>
                            <comment id="882248" author="judy.han" created="Wed, 15 Apr 2015 21:33:39 +0000"  >&lt;p&gt;oops, sorry, my bad. please close the ticket.&lt;/p&gt;</comment>
                            <comment id="882246" author="adam.midvidy" created="Wed, 15 Apr 2015 21:32:12 +0000"  >&lt;p&gt;Hi Judy,&lt;/p&gt;

&lt;p&gt;It appears you are dropping the collection &quot;insert_test.col1&quot; on line 25. As such the behavior you are seeing is expected.&lt;/p&gt;

&lt;p&gt;Adam&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|hs84gn:</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>
                                                                                                                                                                                                                                                                    <customfield id="customfield_10557" key="com.pyxis.greenhopper.jira:gh-sprint">
                        <customfieldname>Sprint</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue id="476">Platform 2 04/24/15</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>