Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-2539

collection::find_one_and_* may raise exception with unacknowledged write concern

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.7.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      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);
      }
      

            Assignee:
            ezra.chung@mongodb.com Ezra Chung
            Reporter:
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: