Example #1
0
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);
}
Example #2
0
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());
}