@@ -39,25 +39,30 @@ options::bulk_write make_bulk_write_options(const options::insert& insert_option
39
39
} // namespace
40
40
41
41
insert_many_builder::insert_many_builder (const options::insert& options)
42
- : _writes{make_bulk_write_options (options)}, _inserted_ids{}, _index{0 } {};
42
+ : _writes{make_bulk_write_options (options)}, _inserted_ids{} {
43
+ }
43
44
44
45
void insert_many_builder::operator ()(const bsoncxx::document::view& doc) {
46
+ bsoncxx::builder::stream::document id_doc;
45
47
if (!doc[" _id" ]) {
46
- bsoncxx::builder::stream::document new_document;
47
- new_document << " _id" << bsoncxx::oid () << bsoncxx::builder::stream::concatenate (doc);
48
+ id_doc << " _id" << bsoncxx::oid{};
48
49
50
+ bsoncxx::builder::stream::document new_document;
51
+ new_document << bsoncxx::builder::stream::concatenate (id_doc.view ())
52
+ << bsoncxx::builder::stream::concatenate (doc);
49
53
_writes.append (model::insert_one{new_document.view ()});
50
- _inserted_ids.emplace (_index++, new_document.view ()[" _id" ]);
51
54
} else {
55
+ id_doc << " _id" << doc[" _id" ].get_value ();
56
+
52
57
_writes.append (model::insert_one{doc});
53
- _inserted_ids.emplace (_index++, doc[" _id" ]);
54
58
}
59
+ _inserted_ids.append (id_doc.view ());
55
60
};
56
61
57
62
stdx::optional<result::insert_many> insert_many_builder::insert (collection* col) const {
58
63
auto val = col->bulk_write (_writes).value ();
59
64
result::bulk_write res{std::move (val)};
60
- stdx::optional<result::insert_many> result{{std::move (res), std::move ( _inserted_ids)}};
65
+ stdx::optional<result::insert_many> result{{std::move (res), _inserted_ids. view ( )}};
61
66
return result;
62
67
};
63
68
0 commit comments