[CXX-2539] collection::find_one_and_* may raise exception with unacknowledged write concern Created: 28/Jun/22  Updated: 28/Oct/23  Resolved: 28/Jun/22

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.7.0

Type: Bug Priority: Unknown
Reporter: Kevin Albertson Assignee: Ezra Chung
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CXX-2347 Allow hint for unacknowledged writes ... Closed

 Description   

Summary

The collection methods find_one_and_update, find_one_and_delete, and find_one_and_replace raise an unexpected exception when an unacknowledged write concern is used.

Background

This is caused by attempting to read a non-existent value from the reply.

How to Reproduce

Here is an example test to reproduce:

TEST_CASE("find_one_and_update works with unacknowledged write concern", "[collection]") {
    instance::current();
    mongocxx::client client{uri{}, test_util::add_test_server_api()};
    mongocxx::write_concern wc;
    wc.acknowledge_level(mongocxx::write_concern::level::k_unacknowledged);
 
    auto collection = client["fam_wc"]["collection"];
    collection.drop();
    collection.insert_one(make_document(kvp("x", 1)));
 
    stdx::optional<bsoncxx::document::value> doc;
    mongocxx::options::find_one_and_update find_one_and_update_opts;
    find_one_and_update_opts.write_concern(wc);
    // find_one_and_update gets an exception with message:
    // "cannot return the type of uninitialized element: unset document::element"
    doc = collection.find_one_and_update(
        {}, make_document(kvp("$set", make_document(kvp("x", 2)))), find_one_and_update_opts);
    REQUIRE(!doc);
}



 Comments   
Comment by Kevin Albertson [ 28/Jun/22 ]

Resolved by CXX-2347.

Generated at Wed Feb 07 22:06:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.