Skip to content

Commit 7ea4163

Browse files
committed
example for ignoring "ns not found" error
1 parent 7921e56 commit 7ea4163

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libmongoc/doc/mongoc_collection_drop_with_opts.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ This function requests that a ``collection`` be dropped, including all indexes a
3030

3131
If no write concern is provided in ``opts``, the collection's write concern is used.
3232

33+
If the collection does not exist, the server responds with an "ns not found" error. It is safe to ignore this error; set the :ref:`Error API Version <error_api_version>` to 2 and ignore server error code 26:
34+
35+
.. code-block:: c
36+
37+
mongoc_client_t *client;
38+
mongoc_collection_t *collection;
39+
bson_error_t error;
40+
bool r;
41+
42+
client = mongoc_client_new (NULL);
43+
mongoc_client_set_error_api (client, 2);
44+
collection = mongoc_client_get_collection (client, "db", "collection");
45+
r = mongoc_collection_drop_with_opts (collection, NULL /* opts */, &error);
46+
if (r) {
47+
printf ("Dropped.\n");
48+
} else {
49+
printf ("Error message: %s\n", error.message);
50+
if (error.domain == MONGOC_ERROR_SERVER && error.code == 26) {
51+
printf ("Ignoring 'ns not found' error\n");
52+
} else {
53+
fprintf (stderr, "Unrecognized error!\n");
54+
}
55+
}
56+
57+
mongoc_collection_destroy (collection);
58+
mongoc_client_destroy (client);
59+
60+
In MongoDB 3.0 and older, the "ns not found" error code is the generic MONGOC_ERROR_QUERY_FAILURE; in this case check whether the error message is equal to the string "ns not found".
61+
3362
Errors
3463
------
3564

0 commit comments

Comments
 (0)