MONGOC_FIND_AN...FY_OPTS_APPEND(3)
hey bestie, welcome to the libmongoc manual page for mongoc_find_and_modify_opts_append(3) π this is the one where you get to slide extra options into your findAndModify command, and honestly? iconic behavior.
β¨ SYNOPSIS β¨
bool
mongoc_find_and_modify_opts_append (mongoc_find_and_modify_opts_t *opts,
const bson_t *extra);
π PARAMETERS π
optsβ Amongoc_find_and_modify_opts_t. this is your find-and-modify options bestie, the main character.extraβ Abson_twith fields and values to append directly to thefindAndModifycommand sent to the server. lowkey the whole point of this function, ngl.
π¦ DESCRIPTION π¦
okay so this function adds arbitrary options to the findAndModify command. that's it, that's the vibe. π
extra does not have to remain valid after calling this function. so don't stress about keeping it alive forever β once you've called this, it's done its job.
extra may be NULL or a BSON document with additional command options:
writeConcernβ Construct amongoc_write_concern_tand usemongoc_write_concern_append()to add the write concern toopts. See the example code formongoc_client_write_command_with_opts(). π«Ά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 from collection, and usemongoc_client_session_append()to add the session toopts. See the example code formongoc_client_session_t. β¨hintβ A document or string that specifies the index to use to support the query predicate. giving the server a little hint-hint, iykyk.letβ A BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.commentβ Abson_value_tspecifying the comment to attach to this command. The comment will appear in log messages, profiler output, andcurrentOpoutput. Requires MongoDB 4.4 or later. π£οΈ
π RETURNS π
Returns true on success. If any arguments are invalid, returns false and logs an error. so, y'know, do a little vibe check on your args before you send it. π
π§ APPENDING OPTIONS TO FINDANDMODIFY π§
the example lives in opts.c, and it is serving real βI know what I'm doingβ energy:
void
fam_opts(mongoc_collection_t *collection)
{
mongoc_find_and_modify_opts_t *opts;
bson_t reply;
bson_t *update;
bson_error_t error;
bson_t query = BSON_INITIALIZER;
mongoc_write_concern_t *wc;
bson_t extra = BSON_INITIALIZER;
bool success;
/* Find Zlatan Ibrahimovic, the striker */
BSON_APPEND_UTF8(&query, "firstname", "Zlatan");
BSON_APPEND_UTF8(&query, "lastname", "Ibrahimovic");
BSON_APPEND_UTF8(&query, "profession", "Football player");
/* Bump his age */
update = BCON_NEW("$inc", "{", "age", BCON_INT32(1), "}");
opts = mongoc_find_and_modify_opts_new();
mongoc_find_and_modify_opts_set_update(opts, update);
/* Abort if the operation takes too long. */
mongoc_find_and_modify_opts_set_max_time_ms(opts, 100);
/* Set write concern w: 2 */
wc = mongoc_write_concern_new();
mongoc_write_concern_set_w(wc, 2);
mongoc_write_concern_append(wc, &extra);
/* Some future findAndModify option the driver doesn't support conveniently
*/
BSON_APPEND_INT32(&extra, "futureOption", 42);
mongoc_find_and_modify_opts_append(opts, &extra);
success = mongoc_collection_find_and_modify_with_opts(collection, &query, opts, &reply, &error);
if (success) {
char *str;
str = bson_as_canonical_extended_json(&reply, NULL);
printf("%s\n", str);
bson_free(str);
} else {
fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, (int)(__LINE__));
}
bson_destroy(&reply);
bson_destroy(&extra);
bson_destroy(update);
bson_destroy(&query);
mongoc_write_concern_destroy(wc);
mongoc_find_and_modify_opts_destroy(opts);
}
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
mwah π β this page came from libmongoc, version 2.5.6, dated Oct 09, 202. slay, go forth, and append those options, bestie πβ¨
π¦ 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