Skip to content

Commit 90c72a8

Browse files
committed
CDRIVER-709 add MONGOC_ERROR_DUPLICATE_KEY
1 parent 1cf7ef1 commit 90c72a8

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ New features and bug fixes:
2020
* connectTimeoutMS timer now begins after DNS resolution, and resets
2121
for each interface attempted (e.g., if the driver first tries IPv6,
2222
then IPv4).
23+
* New error code MONGOC_ERROR_DUPLICATE_KEY.
2324
* mongoc_collection_find no longer treats the "filter" key specially in
2425
queries - querying for a document with a key named "filter" is the same
2526
now as any other key.

doc/mongoc_errors.page

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@
195195
<p>You tried to use a command option the server does not support.</p>
196196
</td>
197197
</tr>
198+
<tr>
199+
<td />
200+
<td>
201+
<p><code>MONGOC_ERROR_DUPLICATE_KEY</code></p>
202+
</td>
203+
<td>
204+
<p>An insert or update failed because because of a duplicate <code>_id</code> or other unique-index violation.</p>
205+
</td>
206+
</tr>
198207
<tr>
199208
<td>
200209
<p><em style="strong"><code>MONGOC_ERROR_COMMAND</code></em></p>

src/mongoc/mongoc-error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ typedef enum
108108
MONGOC_ERROR_PROTOCOL_ERROR = 17,
109109

110110
MONGOC_ERROR_WRITE_CONCERN_ERROR = 64,
111+
112+
MONGOC_ERROR_DUPLICATE_KEY = 11000,
111113
} mongoc_error_code_t;
112114

113115

src/mongoc/mongoc-write-command.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ _mongoc_write_result_merge_arrays (uint32_t offset,
5959
bson_t *dest,
6060
bson_iter_t *iter);
6161

62+
static bool
63+
_is_duplicate_key_error (int32_t code)
64+
{
65+
return code == 11000 ||
66+
code == 16460 || /* see SERVER-11493 */
67+
code == 11001 || /* duplicate key for updates before 2.6 */
68+
code == 12582; /* mongos before 2.6 */
69+
}
70+
71+
6272
void
6373
_mongoc_write_command_insert_append (mongoc_write_command_t *command,
6474
const bson_t *document)
@@ -1590,6 +1600,10 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
15901600
code = bson_iter_int32 (&iter);
15911601
}
15921602

1603+
if (_is_duplicate_key_error (code)) {
1604+
code = MONGOC_ERROR_DUPLICATE_KEY;
1605+
}
1606+
15931607
if (code || err) {
15941608
if (!err) {
15951609
err = "unknown error";

tests/test-mongoc-collection.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,6 +4106,27 @@ test_index_with_collation_ok (void)
41064106
test_index_with_collation (WIRE_VERSION_COLLATION);
41074107
}
41084108

4109+
static void
4110+
test_insert_duplicate_key (void)
4111+
{
4112+
mongoc_client_t *client;
4113+
mongoc_collection_t *collection;
4114+
bson_error_t error;
4115+
4116+
client = test_framework_client_new ();
4117+
collection = get_test_collection (client, "test_insert_duplicate_key");
4118+
mongoc_collection_insert (collection, MONGOC_INSERT_NONE,
4119+
tmp_bson ("{'_id': 1}"), NULL, NULL);
4120+
4121+
ASSERT (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE,
4122+
tmp_bson ("{'_id': 1}"), NULL, &error));
4123+
ASSERT_CMPINT (error.domain, ==, MONGOC_ERROR_COMMAND);
4124+
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_DUPLICATE_KEY);
4125+
4126+
mongoc_collection_destroy (collection);
4127+
mongoc_client_destroy (client);
4128+
}
4129+
41094130
void
41104131
test_collection_install (TestSuite *suite)
41114132
{
@@ -4193,4 +4214,5 @@ test_collection_install (TestSuite *suite)
41934214
TestSuite_AddFull (suite, "/Collection/command_fully_qualified", test_command_fq, NULL, NULL, test_framework_skip_if_mongos);
41944215
TestSuite_AddLive (suite, "/Collection/get_index_info", test_get_index_info);
41954216
TestSuite_Add (suite, "/Collection/find_indexes/error", test_find_indexes_err);
4217+
TestSuite_AddLive (suite, "/Collection/insert/duplicate_key", test_insert_duplicate_key);
41964218
}

0 commit comments

Comments
 (0)