File tree 3 files changed +27
-6
lines changed
3 files changed +27
-6
lines changed Original file line number Diff line number Diff line change 14
14
15
15
#include < mongocxx/result/insert_one.hpp>
16
16
17
+ #include < bsoncxx/builder/stream/array.hpp>
18
+
17
19
#include < mongocxx/config/private/prelude.hh>
18
20
19
21
namespace mongocxx {
20
22
MONGOCXX_INLINE_NAMESPACE_BEGIN
21
23
namespace result {
22
24
23
- insert_one::insert_one (result::bulk_write result, bsoncxx::types::value generated_id)
24
- : _result(std::move(result)), _generated_id(std::move(generated_id)) {
25
+ insert_one::insert_one (result::bulk_write result, bsoncxx::types::value inserted_id)
26
+ : _result(std::move(result)),
27
+ _inserted_id_owned (bsoncxx::builder::stream::array{} << inserted_id
28
+ << bsoncxx::builder::stream::finalize),
29
+ _inserted_id(_inserted_id_owned.view()[0].get_value()) {
25
30
}
26
31
27
32
const result::bulk_write& insert_one::result () const {
28
33
return _result;
29
34
}
30
35
31
36
const bsoncxx::types::value& insert_one::inserted_id () const {
32
- return _generated_id ;
37
+ return _inserted_id ;
33
38
}
34
39
35
40
} // namespace result
Original file line number Diff line number Diff line change 14
14
15
15
#pragma once
16
16
17
+ #include < bsoncxx/array/value.hpp>
17
18
#include < bsoncxx/types.hpp>
18
19
#include < bsoncxx/types/value.hpp>
19
20
#include < mongocxx/result/bulk_write.hpp>
@@ -28,7 +29,7 @@ namespace result {
28
29
class MONGOCXX_API insert_one {
29
30
public:
30
31
// This constructor is public for testing purposes only
31
- insert_one (result::bulk_write result, bsoncxx::types::value generated_id );
32
+ insert_one (result::bulk_write result, bsoncxx::types::value inserted_id );
32
33
33
34
// /
34
35
// / Returns the bulk write result for this insert operation.
@@ -40,13 +41,18 @@ class MONGOCXX_API insert_one {
40
41
// /
41
42
// / Gets the _id of the inserted document.
42
43
// /
43
- // / @return The value of the _id field for inserted document.
44
+ // / @return The value of the _id field for the inserted document.
44
45
// /
45
46
const bsoncxx::types::value& inserted_id () const ;
46
47
47
48
private:
48
49
result::bulk_write _result;
49
- bsoncxx::types::value _generated_id;
50
+
51
+ // Array with a single element, containing the value of the _id field for the inserted document.
52
+ bsoncxx::array::value _inserted_id_owned;
53
+
54
+ // Points into _inserted_id_owned.
55
+ bsoncxx::types::value _inserted_id;
50
56
};
51
57
52
58
} // namespace result
Original file line number Diff line number Diff line change @@ -104,6 +104,16 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
104
104
REQUIRE (i == 1 );
105
105
}
106
106
107
+ SECTION (" insert_one returns correct result object" , " [collection]" ) {
108
+ stdx::string_view expected_id{" foo" };
109
+
110
+ auto result = coll.insert_one (document{} << " _id" << expected_id << finalize);
111
+ REQUIRE (result);
112
+ REQUIRE (result->result ().inserted_count () == 1 );
113
+ REQUIRE (result->inserted_id ().type () == bsoncxx::type::k_utf8);
114
+ REQUIRE (result->inserted_id ().get_utf8 ().value == expected_id);
115
+ }
116
+
107
117
SECTION (" insert and read multiple documents" , " [collection]" ) {
108
118
document b1;
109
119
document b2;
You can’t perform that action at this time.
0 commit comments