@@ -18,6 +18,30 @@ inline namespace v_noabi {
18
18
using bsoncxx::builder::basic::kvp;
19
19
using bsoncxx::builder::basic::make_document;
20
20
21
+ struct collection_deleter {
22
+ void operator ()(mongoc_collection_t * ptr) noexcept {
23
+ libmongoc::collection_destroy (ptr);
24
+ }
25
+ };
26
+
27
+ using collection_ptr = std::unique_ptr<mongoc_collection_t , collection_deleter>;
28
+
29
+ // `copy_and_apply_default_rw_concerns` copies the mongoc_collection_t and applies a default
30
+ // readConcern and writeConcern. Used to prevent sending a readConcern or writeConcern.
31
+ static collection_ptr copy_and_apply_default_rw_concerns (mongoc_collection_t * coll) {
32
+ auto * wc_default = libmongoc::write_concern_new ();
33
+ auto * rc_default = libmongoc::read_concern_new ();
34
+ auto coll_copy = libmongoc::collection_copy (coll);
35
+
36
+ mongoc_collection_set_read_concern (coll_copy, rc_default);
37
+ mongoc_collection_set_write_concern (coll_copy, wc_default);
38
+
39
+ libmongoc::read_concern_destroy (rc_default);
40
+ libmongoc::write_concern_destroy (wc_default);
41
+
42
+ return collection_ptr (coll_copy);
43
+ }
44
+
21
45
class search_index_view ::impl {
22
46
public:
23
47
impl (mongoc_collection_t * collection, mongoc_client_t * client)
@@ -56,8 +80,9 @@ class search_index_view::impl {
56
80
57
81
libbson::scoped_bson_t opts_bson (opts_doc.view ());
58
82
83
+ auto coll_copy = copy_and_apply_default_rw_concerns (_coll);
59
84
return libmongoc::collection_aggregate (
60
- _coll , mongoc_query_flags_t (), stages.bson (), opts_bson.bson (), rp_ptr);
85
+ coll_copy. get () , mongoc_query_flags_t (), stages.bson (), opts_bson.bson (), rp_ptr);
61
86
}
62
87
63
88
std::string create_one (const client_session* session, const search_index_model& model) {
@@ -107,8 +132,9 @@ class search_index_view::impl {
107
132
libbson::scoped_bson_t command_bson{command};
108
133
libbson::scoped_bson_t opts_bson{opts_doc.view ()};
109
134
135
+ auto coll_copy = copy_and_apply_default_rw_concerns (_coll);
110
136
auto result = libmongoc::collection_write_command_with_opts (
111
- _coll , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
137
+ coll_copy. get () , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
112
138
113
139
if (!result) {
114
140
throw_exception<operation_exception>(reply.steal (), error);
@@ -133,8 +159,9 @@ class search_index_view::impl {
133
159
libbson::scoped_bson_t opts_bson{opts_doc.view ()};
134
160
bson_error_t error;
135
161
162
+ auto coll_copy = copy_and_apply_default_rw_concerns (_coll);
136
163
bool result = libmongoc::collection_write_command_with_opts (
137
- _coll , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
164
+ coll_copy. get () , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
138
165
139
166
const uint32_t serverErrorNamespaceNotFound = 26 ;
140
167
if (error.domain == MONGOC_ERROR_QUERY && error.code == serverErrorNamespaceNotFound) {
@@ -167,8 +194,9 @@ class search_index_view::impl {
167
194
libbson::scoped_bson_t opts_bson{opts_doc.view ()};
168
195
bson_error_t error;
169
196
197
+ auto coll_copy = copy_and_apply_default_rw_concerns (_coll);
170
198
bool result = libmongoc::collection_write_command_with_opts (
171
- _coll , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
199
+ coll_copy. get () , command_bson.bson (), opts_bson.bson (), reply.bson_for_init (), &error);
172
200
173
201
if (!result) {
174
202
throw_exception<operation_exception>(reply.steal (), error);
0 commit comments