Skip to content

CDRIVER-3054 fix docs and test mongoc_collection_get_last_error #1782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/libmongoc/doc/mongoc_collection_get_last_error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
mongoc_collection_get_last_error()
==================================

.. warning::
.. deprecated:: 1.9.0

Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a replacement/recommendation to be documented with the deprecation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not currently. I do not plan to add an alternative. mongoc_collection_get_last_error has had the deprecation attribute since 1.9.0. GitHub code search shows no use of mongoc_collection_get_last_error, despite showing use of mongoc_collection_remove, mongoc_collection_insert, and mongoc_collection_update. So I expect this is non-disruptive. If there are reports requesting re-adding, a better named alternative could be considered (mongoc_collection_get_last_write_result?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily a dedicated API to replace it, but is there a way to get the same information that this function provided?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Updated deprecation to note other write functions that return write results.

To get write results from write operations, instead use:

- :symbol:`mongoc_collection_update_one`
- :symbol:`mongoc_collection_update_many`
- :symbol:`mongoc_collection_replace_one`
- :symbol:`mongoc_collection_delete_one`
- :symbol:`mongoc_collection_delete_many`
- :symbol:`mongoc_collection_insert_one`
- :symbol:`mongoc_collection_insert_many`
- :symbol:`mongoc_bulkwrite_t`
- :symbol:`mongoc_bulk_operation_t`


Synopsis
--------

Expand All @@ -19,12 +35,17 @@ Parameters
Description
-----------

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

A write_concern must be at least ``MONGOC_WRITE_CONCERN_W_DEFAULT`` in last command execution for this to be available.
- :symbol:`mongoc_collection_update`
- :symbol:`mongoc_collection_remove`
- :symbol:`mongoc_collection_delete`
- :symbol:`mongoc_collection_insert_bulk`
- :symbol:`mongoc_collection_insert`

Returns
-------

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

107 changes: 107 additions & 0 deletions src/libmongoc/tests/test-mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -5707,6 +5707,112 @@ test_insert_one_reports_id (void)

#undef ASSERT_INDEX_EXISTS

static void
test_get_last_error (void)
{
mongoc_client_t *client = test_framework_new_default_client ();
mongoc_collection_t *coll = get_test_collection (client, "test_get_last_error");
bson_error_t error;
bool ok;

// Test mongoc_collection_update:
{
mongoc_collection_drop (coll, NULL);

// Clear error:
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
ASSERT (!mongoc_collection_get_last_error (coll));

// Insert a document to remove:
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);

// Update:
ok = mongoc_collection_update (
coll, MONGOC_UPDATE_NONE, tmp_bson ("{}"), tmp_bson ("{'$set': {'foo': 'bar'}}"), NULL, &error);
ASSERT_OR_PRINT (ok, error);
const bson_t *gle = mongoc_collection_get_last_error (coll);
ASSERT_MATCH (
gle,
BSON_STR (
{"nInserted" : 0, "nMatched" : 1, "nModified" : 1, "nRemoved" : 0, "nUpserted" : 0, "writeErrors" : []}));
}

// Test mongoc_collection_remove:
{
mongoc_collection_drop (coll, NULL);

// Clear error:
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
ASSERT (!mongoc_collection_get_last_error (coll));

// Insert a document to remove:
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);

ok = mongoc_collection_remove (coll, MONGOC_REMOVE_NONE, tmp_bson ("{}"), NULL, &error);
ASSERT_OR_PRINT (ok, error);
const bson_t *gle = mongoc_collection_get_last_error (coll);
ASSERT_MATCH (
gle,
BSON_STR (
{"nInserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 1, "nUpserted" : 0, "writeErrors" : []}));
}

// Test mongoc_collection_delete:
{
mongoc_collection_drop (coll, NULL);

// Clear error:
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
ASSERT (!mongoc_collection_get_last_error (coll));

// Insert a document to delete:
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, tmp_bson ("{'_id': 0}"), NULL, NULL, &error), error);

ok = mongoc_collection_delete (coll, MONGOC_DELETE_NONE, tmp_bson ("{}"), NULL, &error);
ASSERT_OR_PRINT (ok, error);
const bson_t *gle = mongoc_collection_get_last_error (coll);
ASSERT_MATCH (
gle,
BSON_STR (
{"nInserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 1, "nUpserted" : 0, "writeErrors" : []}));
}

// Test mongoc_collection_insert_bulk:
{
mongoc_collection_drop (coll, NULL);

// Clear error:
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
ASSERT (!mongoc_collection_get_last_error (coll));

bson_t *docs[] = {tmp_bson ("{'_id': 1}")};
ok = mongoc_collection_insert_bulk (coll, MONGOC_INSERT_NONE, (const bson_t **) docs, 1u, NULL, &error);
ASSERT_OR_PRINT (ok, error);
const bson_t *gle = mongoc_collection_get_last_error (coll);
ASSERT_MATCH (
gle,
BSON_STR (
{"nInserted" : 1, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "nUpserted" : 0, "writeErrors" : []}));
}

// Test mongoc_collection_insert:
{
mongoc_collection_drop (coll, NULL);

// Clear error:
ASSERT_OR_PRINT (mongoc_collection_command_simple (coll, tmp_bson ("{'ping': 1}"), NULL, NULL, &error), error);
ASSERT (!mongoc_collection_get_last_error (coll));

ok = mongoc_collection_insert (coll, MONGOC_INSERT_NONE, tmp_bson ("{'_id': 1}"), NULL, &error);
ASSERT_OR_PRINT (ok, error);
const bson_t *gle = mongoc_collection_get_last_error (coll);
ASSERT_MATCH (gle, BSON_STR ({"insertedCount" : 1, "insertedId" : 1}));
}

mongoc_collection_destroy (coll);
mongoc_client_destroy (client);
}

void
test_collection_install (TestSuite *suite)
{
Expand Down Expand Up @@ -5866,4 +5972,5 @@ test_collection_install (TestSuite *suite)
// requires failpoint
test_framework_skip_if_no_failpoint);
TestSuite_AddLive (suite, "/Collection/insert_one_reports_id", test_insert_one_reports_id);
TestSuite_AddLive (suite, "/Collection/get_last_error", test_get_last_error);
}