Skip to content

Commit 8f24109

Browse files
authored
CDRIVER-3054 fix docs and test mongoc_collection_get_last_error (#1782)
* add missing deprecation warning to `mongoc_collection_get_last_error` doc * revise description of `mongoc_collection_get_last_error` Note applicable operations. Remove note about unacknowledged writes. An unacknowledged write concern may still produce a non-empty document (e.g. on a network error). * clarify error may be invalidated on next collection operation * test `mongoc_collection_get_last_error` * document alternatives for getting write results from write operations
1 parent 3b5d064 commit 8f24109

File tree

2 files changed

+131
-3
lines changed

2 files changed

+131
-3
lines changed

src/libmongoc/doc/mongoc_collection_get_last_error.rst

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
mongoc_collection_get_last_error()
44
==================================
55

6+
.. warning::
7+
.. deprecated:: 1.9.0
8+
9+
To get write results from write operations, instead use:
10+
11+
- :symbol:`mongoc_collection_update_one`
12+
- :symbol:`mongoc_collection_update_many`
13+
- :symbol:`mongoc_collection_replace_one`
14+
- :symbol:`mongoc_collection_delete_one`
15+
- :symbol:`mongoc_collection_delete_many`
16+
- :symbol:`mongoc_collection_insert_one`
17+
- :symbol:`mongoc_collection_insert_many`
18+
- :symbol:`mongoc_bulkwrite_t`
19+
- :symbol:`mongoc_bulk_operation_t`
20+
21+
622
Synopsis
723
--------
824

@@ -19,12 +35,17 @@ Parameters
1935
Description
2036
-----------
2137

22-
The mongoc_collection_get_last_error() function returns a bulk result. See `Bulk Write Operations <bulk_>`_ for examples of bulk results.
38+
:symbol:`mongoc_collection_get_last_error` returns write results from some operations:
2339

24-
A write_concern must be at least ``MONGOC_WRITE_CONCERN_W_DEFAULT`` in last command execution for this to be available.
40+
- :symbol:`mongoc_collection_update`
41+
- :symbol:`mongoc_collection_remove`
42+
- :symbol:`mongoc_collection_delete`
43+
- :symbol:`mongoc_collection_insert_bulk`
44+
- :symbol:`mongoc_collection_insert`
2545

2646
Returns
2747
-------
2848

29-
A :symbol:`bson:bson_t` that should not be modified or ``NULL``.
49+
A :symbol:`bson:bson_t` that should not be modified or ``NULL``. The returned :symbol:`bson:bson_t` is may be
50+
invalidated by the next operation on ``collection``.
3051

src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5687,6 +5687,112 @@ test_insert_one_reports_id (void)
56875687

56885688
#undef ASSERT_INDEX_EXISTS
56895689

5690+
static void
5691+
test_get_last_error (void)
5692+
{
5693+
mongoc_client_t *client = test_framework_new_default_client ();
5694+
mongoc_collection_t *coll = get_test_collection (client, "test_get_last_error");
5695+
bson_error_t error;
5696+
bool ok;
5697+
5698+
// Test mongoc_collection_update:
5699+
{
5700+
mongoc_collection_drop (coll, NULL);
5701+
5702+
// Clear error:
5703+
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
5704+
ASSERT (!mongoc_collection_get_last_error (coll));
5705+
5706+
// Insert a document to remove:
5707+
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);
5708+
5709+
// Update:
5710+
ok = mongoc_collection_update (
5711+
coll, MONGOC_UPDATE_NONE, tmp_bson ("{}"), tmp_bson ("{'$set': {'foo': 'bar'}}"), NULL, &error);
5712+
ASSERT_OR_PRINT (ok, error);
5713+
const bson_t *gle = mongoc_collection_get_last_error (coll);
5714+
ASSERT_MATCH (
5715+
gle,
5716+
BSON_STR (
5717+
{"nInserted" : 0, "nMatched" : 1, "nModified" : 1, "nRemoved" : 0, "nUpserted" : 0, "writeErrors" : []}));
5718+
}
5719+
5720+
// Test mongoc_collection_remove:
5721+
{
5722+
mongoc_collection_drop (coll, NULL);
5723+
5724+
// Clear error:
5725+
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
5726+
ASSERT (!mongoc_collection_get_last_error (coll));
5727+
5728+
// Insert a document to remove:
5729+
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);
5730+
5731+
ok = mongoc_collection_remove (coll, MONGOC_REMOVE_NONE, tmp_bson ("{}"), NULL, &error);
5732+
ASSERT_OR_PRINT (ok, error);
5733+
const bson_t *gle = mongoc_collection_get_last_error (coll);
5734+
ASSERT_MATCH (
5735+
gle,
5736+
BSON_STR (
5737+
{"nInserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 1, "nUpserted" : 0, "writeErrors" : []}));
5738+
}
5739+
5740+
// Test mongoc_collection_delete:
5741+
{
5742+
mongoc_collection_drop (coll, NULL);
5743+
5744+
// Clear error:
5745+
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
5746+
ASSERT (!mongoc_collection_get_last_error (coll));
5747+
5748+
// Insert a document to delete:
5749+
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);
5750+
5751+
ok = mongoc_collection_delete (coll, MONGOC_DELETE_NONE, tmp_bson ("{}"), NULL, &error);
5752+
ASSERT_OR_PRINT (ok, error);
5753+
const bson_t *gle = mongoc_collection_get_last_error (coll);
5754+
ASSERT_MATCH (
5755+
gle,
5756+
BSON_STR (
5757+
{"nInserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 1, "nUpserted" : 0, "writeErrors" : []}));
5758+
}
5759+
5760+
// Test mongoc_collection_insert_bulk:
5761+
{
5762+
mongoc_collection_drop (coll, NULL);
5763+
5764+
// Clear error:
5765+
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
5766+
ASSERT (!mongoc_collection_get_last_error (coll));
5767+
5768+
bson_t *docs[] = {tmp_bson ("{'_id': 1}")};
5769+
ok = mongoc_collection_insert_bulk (coll, MONGOC_INSERT_NONE, (const bson_t **) docs, 1u, NULL, &error);
5770+
ASSERT_OR_PRINT (ok, error);
5771+
const bson_t *gle = mongoc_collection_get_last_error (coll);
5772+
ASSERT_MATCH (
5773+
gle,
5774+
BSON_STR (
5775+
{"nInserted" : 1, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "nUpserted" : 0, "writeErrors" : []}));
5776+
}
5777+
5778+
// Test mongoc_collection_insert:
5779+
{
5780+
mongoc_collection_drop (coll, NULL);
5781+
5782+
// Clear error:
5783+
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
5784+
ASSERT (!mongoc_collection_get_last_error (coll));
5785+
5786+
ok = mongoc_collection_insert (coll, MONGOC_INSERT_NONE, tmp_bson ("{'_id': 1}"), NULL, &error);
5787+
ASSERT_OR_PRINT (ok, error);
5788+
const bson_t *gle = mongoc_collection_get_last_error (coll);
5789+
ASSERT_MATCH (gle, BSON_STR ({"insertedCount" : 1, "insertedId" : 1}));
5790+
}
5791+
5792+
mongoc_collection_destroy (coll);
5793+
mongoc_client_destroy (client);
5794+
}
5795+
56905796
void
56915797
test_collection_install (TestSuite *suite)
56925798
{
@@ -5846,4 +5952,5 @@ test_collection_install (TestSuite *suite)
58465952
// requires failpoint
58475953
test_framework_skip_if_no_failpoint);
58485954
TestSuite_AddLive (suite, "/Collection/insert_one_reports_id", test_insert_one_reports_id);
5955+
TestSuite_AddLive (suite, "/Collection/get_last_error", test_get_last_error);
58495956
}

0 commit comments

Comments
 (0)