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 β€” a mongoc_collection_t. like, the collection we're doing this to. main character.
  • selector β€” a bson_t holding the query that matches the document you're updating. this is how we find our target πŸ”
  • replacement β€” a bson_t holding the replacement document. the new fit. πŸ’ƒ
  • reply β€” a maybe-NULL pointer to overwritable storage for a bson_t that'll contain the results. lowkey optional, highkey useful.
  • error β€” an optional location for a bson_error_t, or NULL if 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 a mongoc_write_concern_t and use mongoc_write_concern_append() to add the write concern to opts. check the example code for mongoc_client_write_command_with_opts(), iykyk.
  • sessionId β€” first, construct a mongoc_client_session_t with mongoc_client_start_session(). you can begin a transaction with mongoc_client_session_start_transaction(), optionally with a mongoc_transaction_opt_t that overrides the options inherited from collection, and use mongoc_client_session_append() to add the session to opts. see the example code for mongoc_client_session_t 🫢
  • validate β€” construct a bitwise-or of all desired bson_validate_flags_t. set it to false to skip client-side validation of the BSON documents you provided. (skipping validation? bold. iconic, but bold. πŸ’…)
  • comment β€” a bson_value_t specifying the comment to attach to this command. it'll show up in log messages, profiler output, and currentOp output. requires MongoDB 4.4 or later ⭐
  • bypassDocumentValidation β€” set to true to 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 β€” when true, 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 update command documentation, for more info on the update options πŸ“š
  • mongoc_collection_update_one()
  • mongoc_collection_update_many()

🌈 AUTHOR 🌈

MongoDB, Inc β€” the legends themselves 🫢

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