bsoncxx::stdx::optional<bsoncxx::document::value> collection::find_one_and_delete( bsoncxx::document::view filter, const options::find_one_and_delete& options) { scoped_bson_t bson_filter{filter}; 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; bool r = libmongoc::collection_find_and_modify( _impl->collection_t, bson_filter.bson(), bson_sort.bson(), nullptr, bson_projection.bson(), true, false, false, 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_delete( view_or_value filter, const options::find_one_and_delete& options) { auto opts = libmongoc::find_and_modify_opts_new(); auto opts_cleanup = make_guard([&opts] { libmongoc::find_and_modify_opts_destroy(opts); }); auto flags = ::MONGOC_FIND_AND_MODIFY_REMOVE; if (options.collation()) { scoped_bson_t bson_collation{make_document(kvp("collation", *options.collation()))}; libmongoc::find_and_modify_opts_append(opts, bson_collation.bson()); } if (options.sort()) { libmongoc::find_and_modify_opts_set_sort(opts, scoped_bson_t{*options.sort()}.bson()); } if (options.projection()) { libmongoc::find_and_modify_opts_set_fields(opts, scoped_bson_t{*options.projection()}.bson()); } if (options.max_time()) { libmongoc::find_and_modify_opts_set_max_time_ms(opts, options.max_time()->count()); } libmongoc::find_and_modify_opts_set_flags(opts, flags); return find_and_modify(_get_impl().collection_t, filter, opts); }
stdx::optional<bsoncxx::document::value> collection::find_one_and_delete( view_or_value filter, const options::find_one_and_delete& options) { auto opts = libmongoc::find_and_modify_opts_new(); auto opts_cleanup = make_guard([&opts] { libmongoc::find_and_modify_opts_destroy(opts); }); auto flags = ::MONGOC_FIND_AND_MODIFY_REMOVE; scoped_bson_t bson_sort{options.sort()}; scoped_bson_t bson_projection{options.projection()}; 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()); } // TODO: use options.max_time() when available in the C driver libmongoc::find_and_modify_opts_set_flags(opts, flags); return find_and_modify(_get_impl().collection_t, filter, opts); }
stdx::optional<bsoncxx::document::value> collection::find_one_and_delete( view_or_value filter, const options::find_one_and_delete& 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("remove", true)); if (options.sort()) { options_doc.append(kvp("sort", *options.sort())); } if (options.collation()) { options_doc.append(kvp("collation", *options.collation())); } if (options.projection()) { options_doc.append(kvp("fields", *options.projection())); } if (options.max_time()) { options_doc.append(kvp("maxTimeMS", bsoncxx::types::b_int64{options.max_time()->count()})); } if (options.write_concern()) { options_doc.append(kvp("writeConcern", options.write_concern()->to_document())); } return find_and_modify( _get_impl().collection_t, command_doc.view(), options_doc.view(), options.write_concern()); }