Skip to content

Commit 008862f

Browse files
author
Tess Avitabile
committed
CXX-798 - Fix result::bulk_write::upserted_ids and remove inserted_ids
1 parent 64883e8 commit 008862f

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

examples/mongocxx/bulk_write.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
// limitations under the License.
1414

1515
#include <cstdlib>
16+
#include <iostream>
1617

1718
#include <bsoncxx/builder/stream/document.hpp>
18-
19+
#include <bsoncxx/json.hpp>
1920
#include <mongocxx/client.hpp>
2021
#include <mongocxx/instance.hpp>
2122
#include <mongocxx/uri.hpp>
@@ -98,7 +99,10 @@ int main(int, char**) {
9899
return EXIT_FAILURE;
99100
}
100101

101-
// TODO: print inserted and upserted IDs once CXX-798 is resolved
102+
std::cout << "Upserted IDs" << std::endl;
103+
for (const auto &id : result->upserted_ids()) {
104+
std::cout << "Bulk write index: " << id.first << std::endl << bsoncxx::to_json(id.second) << std::endl;
105+
}
102106

103107
// The collection should contain two copies of {"a": 2}.
104108
auto cursor = coll.find({});

src/mongocxx/result/bulk_write.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ std::int32_t bulk_write::upserted_count() const {
4343
return view()["nUpserted"].get_int32();
4444
}
4545

46-
bsoncxx::document::element bulk_write::inserted_ids() const {
47-
return view()["inserted_ids"];
48-
}
46+
bulk_write::id_map bulk_write::upserted_ids() const {
47+
id_map upserted_ids;
48+
49+
if (!view()["upserted"]) {
50+
return upserted_ids;
51+
}
4952

50-
bsoncxx::document::element bulk_write::upserted_ids() const {
51-
return view()["upserted_ids"];
53+
for (auto&& id : view()["upserted"].get_array().value) {
54+
upserted_ids.emplace(id["index"].get_int32(), id["_id"]);
55+
}
56+
return upserted_ids;
5257
}
5358

5459
bsoncxx::document::view bulk_write::view() const {

src/mongocxx/result/bulk_write.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <mongocxx/config/prelude.hpp>
1818

1919
#include <cstdint>
20+
#include <map>
2021
#include <vector>
2122

2223
#include <bsoncxx/document/value.hpp>
@@ -33,6 +34,8 @@ namespace result {
3334
class MONGOCXX_API bulk_write {
3435

3536
public:
37+
using id_map = std::map<std::size_t, bsoncxx::document::element>;
38+
3639
explicit bulk_write(bsoncxx::document::value raw_response);
3740

3841
///
@@ -70,19 +73,13 @@ class MONGOCXX_API bulk_write {
7073
///
7174
std::int32_t upserted_count() const;
7275

73-
///
74-
/// Gets the ids of the inserted documents.
75-
///
76-
/// @return The values of the _id field for inserted documents.
77-
///
78-
bsoncxx::document::element inserted_ids() const;
79-
8076
///
8177
/// Gets the ids of the upserted documents.
8278
///
83-
/// @return The values of the _id field for upserted documents.
79+
/// @note The returned id_map must not be accessed after the bulk_write object is destroyed.
80+
/// @return A map from bulk write index to _id field for upserted documents.
8481
///
85-
bsoncxx::document::element upserted_ids() const;
82+
id_map upserted_ids() const;
8683

8784
private:
8885
MONGOCXX_PRIVATE bsoncxx::document::view view() const;

src/mongocxx/result/replace_one.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ std::int32_t replace_one::modified_count() const {
3636
}
3737

3838
stdx::optional<bsoncxx::document::element> replace_one::upserted_id() const {
39-
return _result.upserted_ids();
39+
if (_result.upserted_ids().size() == 0) {
40+
return stdx::nullopt;
41+
}
42+
return _result.upserted_ids()[0];
4043
}
4144

4245
} // namespace result

src/mongocxx/result/update.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ std::int32_t update::modified_count() const {
3535
}
3636

3737
stdx::optional<bsoncxx::document::element> update::upserted_id() const {
38-
return _result.upserted_ids();
38+
if (_result.upserted_ids().size() == 0) {
39+
return stdx::nullopt;
40+
}
41+
return _result.upserted_ids()[0];
3942
}
4043

4144
} // namespace result

src/mongocxx/test/collection.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
197197
options::update options;
198198
options.upsert(true);
199199

200-
coll.update_one(b1.view(), update_doc.view(), options);
200+
auto result = coll.update_one(b1.view(), update_doc.view(), options);
201+
REQUIRE(result->upserted_id());
201202

202203
auto updated = coll.find_one({});
203204

@@ -218,7 +219,8 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
218219
options::update options;
219220
options.upsert(true);
220221

221-
coll.update_one(b1.view(), update_doc.view(), options);
222+
auto result = coll.update_one(b1.view(), update_doc.view(), options);
223+
REQUIRE(!(result->upserted_id()));
222224

223225
auto updated = coll.find_one({});
224226

0 commit comments

Comments
 (0)