bsoncxx::stdx::optional<bsoncxx::document::value> collection::find_one_and_replace( bsoncxx::document::view filter, bsoncxx::document::view replacement, const options::find_one_and_replace& options) { scoped_bson_t bson_filter{filter}; scoped_bson_t bson_replacement{replacement}; scoped_bson_t bson_sort{options.sort()}; scoped_bson_t bson_projection{options.projection()}; scoped_bson_t reply; reply.flag_init(); bson_error_t error; options::return_document rd = options.return_document().value_or(options::return_document::k_before); bool r = mongoc_collection_find_and_modify( _impl->collection_t, bson_filter.bson(), bson_sort.bson(), bson_replacement.bson(), bson_projection.bson(), false, options.upsert().value_or(false), rd == options::return_document::k_after, reply.bson(), &error); if (!r) { throw std::runtime_error("baddd"); } bsoncxx::document::view result = reply.view(); if (result["value"].type() == bsoncxx::type::k_null) return bsoncxx::stdx::optional<bsoncxx::document::value>{}; bsoncxx::builder::stream::document b; b << bsoncxx::builder::stream::concatenate{result["value"].get_document()}; return b.extract(); }
stdx::optional<bsoncxx::document::value> collection::find_one_and_replace( view_or_value filter, view_or_value replacement, const options::find_one_and_replace& options) { auto opts = libmongoc::find_and_modify_opts_new(); auto opts_cleanup = make_guard([&opts] { libmongoc::find_and_modify_opts_destroy(opts); }); int flags = ::MONGOC_FIND_AND_MODIFY_NONE; scoped_bson_t bson_replacement{replacement}; scoped_bson_t bson_sort{options.sort()}; scoped_bson_t bson_projection{options.projection()}; libmongoc::find_and_modify_opts_set_update(opts, bson_replacement.bson()); if (options.bypass_document_validation()) { libmongoc::find_and_modify_opts_set_bypass_document_validation( opts, *options.bypass_document_validation()); } if (options.sort()) { libmongoc::find_and_modify_opts_set_sort(opts, bson_sort.bson()); } if (options.projection()) { libmongoc::find_and_modify_opts_set_fields(opts, bson_projection.bson()); } if (options.upsert().value_or(false)) { flags |= ::MONGOC_FIND_AND_MODIFY_UPSERT; } if (options.return_document() == options::return_document::k_after) { flags |= ::MONGOC_FIND_AND_MODIFY_RETURN_NEW; } // TODO: use options.max_time() when available in the C driver libmongoc::find_and_modify_opts_set_flags(opts, static_cast<::mongoc_find_and_modify_flags_t>(flags)); return find_and_modify(_get_impl().collection_t, filter, opts); }
stdx::optional<bsoncxx::document::value> collection::find_one_and_replace( view_or_value filter, view_or_value replacement, const options::find_one_and_replace& options) { bsoncxx::builder::basic::document command_doc; bsoncxx::builder::basic::document options_doc; command_doc.append( kvp("findAndModify", libmongoc::collection_get_name(_get_impl().collection_t))); options_doc.append(kvp("query", filter)); options_doc.append(kvp("update", replacement.view())); if (options.sort()) { options_doc.append(kvp("sort", *options.sort())); } if (options.bypass_document_validation()) { options_doc.append(kvp("bypassDocumentValidation", *options.bypass_document_validation())); } if (options.collation()) { options_doc.append(kvp("collation", *options.collation())); } if (options.projection()) { options_doc.append(kvp("fields", *options.projection())); } if (options.upsert().value_or(false)) { options_doc.append(kvp("upsert", *options.upsert())); } if (options.return_document() == options::return_document::k_after) { options_doc.append(kvp("new", true)); } if (options.max_time()) { options_doc.append(kvp("maxTimeMS", bsoncxx::types::b_int64{options.max_time()->count()})); } return find_and_modify( _get_impl().collection_t, command_doc.view(), options_doc.view(), options.write_concern()); }