MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
insert_many.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <mongocxx/result/insert_many-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <mongocxx/v1/insert_many_result.hpp> // IWYU pragma: export
22
23#include <cstddef>
24#include <cstdint>
25#include <map>
26#include <utility>
27
28#include <mongocxx/collection-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
29
33#include <bsoncxx/types.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
34
36
38
39namespace mongocxx {
40namespace v_noabi {
41namespace result {
42
46class insert_many {
47 public:
48 using id_map = std::map<std::size_t, bsoncxx::v_noabi::document::element>;
49
50 ~insert_many() noexcept = default;
51
52 insert_many(insert_many&& other) noexcept = default;
53
54 insert_many& operator=(insert_many&& other) = default;
55
56 insert_many(insert_many const& other) : _result{other._result}, _inserted_ids{other._inserted_ids} {
57 this->sync_id_map();
58 }
59
60 insert_many& operator=(insert_many const& other) {
61 if (this != &other) {
62 _result = other._result;
63 _inserted_ids = other._inserted_ids;
64 this->sync_id_map();
65 }
66
67 return *this;
68 }
69
73 /* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() insert_many(v1::insert_many_result opts);
74
78 explicit MONGOCXX_ABI_EXPORT_CDECL() operator v1::insert_many_result() const;
79
83 insert_many(result::bulk_write result, bsoncxx::v_noabi::array::value inserted_ids)
84 : _result{std::move(result)}, _inserted_ids{std::move(inserted_ids)} {
85 this->sync_id_map();
86 }
87
94 return _result;
95 }
96
102 std::int32_t inserted_count() const {
103 return _result.inserted_count();
104 }
105
113 id_map inserted_ids() const {
114 return _id_map;
115 }
116
117 friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(insert_many const& lhs, insert_many const& rhs);
118
119 friend bool operator!=(insert_many const& lhs, insert_many const& rhs) {
120 return !(lhs == rhs);
121 }
122
123 private:
125 bsoncxx::v_noabi::array::value _inserted_ids;
126 id_map _id_map;
127
128 void sync_id_map() {
129 std::size_t index = 0;
130 for (auto const& ele : _inserted_ids) {
131 // v_noabi::array::element -> v1::element::view -> v_noabi::document::element
132 _id_map.emplace(index++, bsoncxx::v_noabi::to_v1(ele));
133 }
134 }
135};
136
137} // namespace result
138} // namespace v_noabi
139} // namespace mongocxx
140
141namespace mongocxx {
142namespace v_noabi {
143
148 return {std::move(v)};
149}
150
157
158} // namespace v_noabi
159} // namespace mongocxx
160
162
Provides bsoncxx::v_noabi::array::element.
A read-only BSON array that owns its underlying buffer.
Definition value.hpp:44
The result of an "insertMany" operation.
Definition insert_many_result.hpp:45
The result of a MongoDB bulk write operation.
Definition bulk_write.hpp:43
The result of a MongoDB insert many operation.
Definition insert_many.hpp:46
operator v1::insert_many_result() const
Convert to the mongocxx::v1 equivalent.
std::int32_t inserted_count() const
Gets the number of documents that were inserted during this operation.
Definition insert_many.hpp:102
id_map inserted_ids() const
Gets the _ids of the inserted documents.
Definition insert_many.hpp:113
v_noabi::result::bulk_write const & result() const
Returns the bulk write result for this insert many operation.
Definition insert_many.hpp:93
Provides bsoncxx::v_noabi::document::element.
Declares mongocxx::v_noabi::result::insert_many.
Provides mongocxx::v1::insert_many_result.
#define MONGOCXX_ABI_EXPORT_CDECL(...)
Equivalent to MONGOCXX_ABI_EXPORT with MONGOCXX_ABI_CDECL.
Definition export.hpp:52
The mongocxx v_noabi macro guard postlude header.
The mongocxx v_noabi macro guard prelude header.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
The top-level namespace within which all bsoncxx library entities are declared.
Declares entities whose ABI stability is guaranteed for documented symbols.
Declares entities representing the result of various commands.
Declares entities whose ABI stability is NOT guaranteed.
bool operator==(std::string str, hint const &hint)
Convenience methods to compare against an index name.
Definition hint.hpp:138
bool operator!=(hint const &hint, std::string str)
Convenience methods to compare against an index name.
Definition hint.hpp:143
v1::bulk_write to_v1(v_noabi::bulk_write v)
Convert to the mongocxx::v1 equivalent of v.
Definition bulk_write.hpp:162
v_noabi::bulk_write from_v1(v1::bulk_write v)
Convert to the mongocxx::v_noabi equivalent of v.
Definition bulk_write.hpp:155
The top-level namespace within which all mongocxx library entities are declared.
Provides entities used to represent BSON types.
Provides bsoncxx::v_noabi::array::value.
Declares mongocxx::v_noabi::collection.
Provides mongocxx::v_noabi::result::bulk_write.