MONGOC_COLLECTION_REPLACE_ONE(3)
π MONGOC_COLLECTION_REPLACE_ONE π
okay bestie, welcome to libmongoc's very own mongoc_collection_replace_one β the function that's literally the "swap the whole doc out" girlie of the collection world. she's giving β¨glow-upβ¨ energy: find the doc, yeet the old one, drop the new one in. periodt.
πΈ SYNOPSIS πΈ
bool
mongoc_collection_replace_one (mongoc_collection_t *collection,
const bson_t *selector,
const bson_t *replacement,
const bson_t *opts,
bson_t *reply,
bson_error_t *error);
π PARAMETERS π
collectionβ amongoc_collection_t. like, the collection we're doing this to. main character.selectorβ abson_tholding the query that matches the document you're updating. this is how we find our target πreplacementβ abson_tholding the replacement document. the new fit. πreplyβ a maybe-NULLpointer to overwritable storage for abson_tthat'll contain the results. lowkey optional, highkey useful.errorβ an optional location for abson_error_t, orNULLif you're feeling brave π
opts can be NULL, or a BSON document with extra command options. here's the whole menu, no cap:
writeConcernβ construct amongoc_write_concern_tand usemongoc_write_concern_append()to add the write concern toopts. check the example code formongoc_client_write_command_with_opts(), iykyk.sessionIdβ first, construct amongoc_client_session_twithmongoc_client_start_session(). you can begin a transaction withmongoc_client_session_start_transaction(), optionally with amongoc_transaction_opt_tthat overrides the options inherited fromcollection, and usemongoc_client_session_append()to add the session toopts. see the example code formongoc_client_session_tπ«Άvalidateβ construct a bitwise-or of all desiredbson_validate_flags_t. set it tofalseto skip client-side validation of the BSON documents you provided. (skipping validation? bold. iconic, but bold. π )commentβ abson_value_tspecifying the comment to attach to this command. it'll show up in log messages, profiler output, andcurrentOpoutput. requires MongoDB 4.4 or later βbypassDocumentValidationβ set totrueto skip server-side schema validation of the provided BSON documents. living dangerously, love that for you π¦collationβ configure textual comparisons. see Setting Collation Order, and the MongoDB Manual entry on Collation. collation requires MongoDB 3.2 or later β otherwise an error is returned. no exceptions, bestie πhintβ a document or string that specifies the index to use to support the query predicate.upsertβ whentrue, creates a new document if no document matches the query. no match? no problem, we invent one β¨letβ a BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.sortβ specify a sort order when matching documents.
π¦ DESCRIPTION π¦
this function shall replace documents in collection that match selector with replacement. simple as that, honestly. π―
if provided, reply will be initialized and populated with the fields matchedCount, modifiedCount, upsertedCount, and optionally upsertedId if applicable. if there's a server error, then reply contains either a writeErrors array with one subdocument, or a writeConcernErrors array. either way, the reply must be freed with bson_destroy(). don't forget, or that memory's just gonna sit there rotting π
π₯ ERRORS π₯
errors are propagated via the error parameter. that's the whole vibe. short and sweet.
π RETURNS π
returns true if successful. returns false and sets error if there are invalid arguments, or a server or network error. π
a write concern timeout or write concern error is considered a failure. like, she said yes but then ghosted you. still a no. π
π SEE ALSO π
- the MongoDB
updatecommand documentation, for more info on the update options π mongoc_collection_update_one()mongoc_collection_update_many()
π AUTHOR π
MongoDB, Inc β the legends themselves π«Ά
β COPYRIGHT β
2009-present, MongoDB, Inc.
and that's the whole page, bestie! β¨ this was libmongoc version 2.5.6, dated Oct 09, 2026 β mwah, go forth and replace responsibly ππ
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms