예제 #1
0
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;

    libmongoc::find_and_modify_opts_set_update(opts, scoped_bson_t{replacement}.bson());

    if (options.bypass_document_validation()) {
        libmongoc::find_and_modify_opts_set_bypass_document_validation(
            opts, *options.bypass_document_validation());
    }

    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.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;
    }

    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,
                                              static_cast<::mongoc_find_and_modify_flags_t>(flags));

    return find_and_modify(_get_impl().collection_t, filter, opts);
}
예제 #2
0
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());
}