stdx::optional<result::insert_one> collection::insert_one(view_or_value document, const options::insert& options) { class bulk_write bulk_op(false); bsoncxx::document::element oid{}; bsoncxx::builder::stream::document new_document; if (!document.view()["_id"]) { new_document << "_id" << bsoncxx::oid(bsoncxx::oid::init_tag); new_document << concatenate(document); bulk_op.append(model::insert_one(new_document.view())); oid = new_document.view()["_id"]; } else { bulk_op.append(model::insert_one(document)); oid = document.view()["_id"]; } if (options.write_concern()) { bulk_op.write_concern(*options.write_concern()); } if (options.bypass_document_validation()) { bulk_op.bypass_document_validation(*options.bypass_document_validation()); } auto result = bulk_write(bulk_op); if (!result) { return stdx::optional<result::insert_one>(); } return stdx::optional<result::insert_one>( result::insert_one(std::move(result.value()), std::move(oid.get_value()))); }
stdx::optional<result::insert_one> collection::insert_one(view_or_value document, const options::insert& options) { // TODO: We should consider making it possible to convert from an options::insert into // an options::bulk_write at the type level, removing the need to re-iterate this code // many times here and below. // // See comments in: https://github.com/mongodb/mongo-cxx-driver/pull/409 options::bulk_write bulk_opts; bulk_opts.ordered(false); if (options.write_concern()) { bulk_opts.write_concern(*options.write_concern()); } if (options.bypass_document_validation()) { bulk_opts.bypass_document_validation(*options.bypass_document_validation()); } class bulk_write bulk_op(bulk_opts); bsoncxx::document::element oid{}; bsoncxx::builder::basic::document new_document; if (!document.view()["_id"]) { new_document.append(kvp("_id", bsoncxx::oid())); new_document.append(concatenate(document)); bulk_op.append(model::insert_one(new_document.view())); oid = new_document.view()["_id"]; } else { bulk_op.append(model::insert_one(document)); oid = document.view()["_id"]; } auto result = bulk_write(bulk_op); if (!result) { return stdx::nullopt; } return stdx::optional<result::insert_one>( result::insert_one(std::move(result.value()), std::move(oid.get_value()))); }
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()); }
BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) { return lhs.view() == View(rhs); }
BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) { return lhs.view() == rhs; }