.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "MONGOC_FIND_AND_MODIFY_OPTS_T" "3" "May 07, 2024" "1.27.1" "libmongoc" .sp find_and_modify abstraction .SH SYNOPSIS .sp \fBmongoc_find_and_modify_opts_t\fP is a builder interface to construct \fI\%the findAndModify command\fP\&. .sp It was created to be able to accommodate new arguments to \fI\%the findAndModify command\fP\&. .sp As of MongoDB 3.2, the \fI\%mongoc_write_concern_t\fP specified on the \fI\%mongoc_collection_t\fP will be used, if any. .SH EXAMPLE .sp flags.c .INDENT 0.0 .INDENT 3.5 .sp .EX void fam_flags (mongoc_collection_t *collection) { mongoc_find_and_modify_opts_t *opts; bson_t reply; bson_error_t error; bson_t query = BSON_INITIALIZER; bson_t *update; bool success; /* Find Zlatan Ibrahimovic, the striker */ BSON_APPEND_UTF8 (&query, \(dqfirstname\(dq, \(dqZlatan\(dq); BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); BSON_APPEND_UTF8 (&query, \(dqprofession\(dq, \(dqFootball player\(dq); BSON_APPEND_INT32 (&query, \(dqage\(dq, 34); BSON_APPEND_INT32 (&query, \(dqgoals\(dq, (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62)); /* Add his football position */ update = BCON_NEW (\(dq$set\(dq, \(dq{\(dq, \(dqposition\(dq, BCON_UTF8 (\(dqstriker\(dq), \(dq}\(dq); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); /* Create the document if it didn\(aqt exist, and return the updated document */ mongoc_find_and_modify_opts_set_flags (opts, MONGOC_FIND_AND_MODIFY_UPSERT | MONGOC_FIND_AND_MODIFY_RETURN_NEW); 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); } bson_destroy (&reply); bson_destroy (update); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); } .EE .UNINDENT .UNINDENT .sp bypass.c .INDENT 0.0 .INDENT 3.5 .sp .EX void fam_bypass (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; bool success; /* Find Zlatan Ibrahimovic, the striker */ BSON_APPEND_UTF8 (&query, \(dqfirstname\(dq, \(dqZlatan\(dq); BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); BSON_APPEND_UTF8 (&query, \(dqprofession\(dq, \(dqFootball player\(dq); /* Bump his age */ update = BCON_NEW (\(dq$inc\(dq, \(dq{\(dq, \(dqage\(dq, BCON_INT32 (1), \(dq}\(dq); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); /* He can still play, even though he is pretty old. */ mongoc_find_and_modify_opts_set_bypass_document_validation (opts, true); 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); } bson_destroy (&reply); bson_destroy (update); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); } .EE .UNINDENT .UNINDENT .sp update.c .INDENT 0.0 .INDENT 3.5 .sp .EX void fam_update (mongoc_collection_t *collection) { mongoc_find_and_modify_opts_t *opts; bson_t *update; bson_t reply; bson_error_t error; bson_t query = BSON_INITIALIZER; bool success; /* Find Zlatan Ibrahimovic */ BSON_APPEND_UTF8 (&query, \(dqfirstname\(dq, \(dqZlatan\(dq); BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); /* Make him a book author */ update = BCON_NEW (\(dq$set\(dq, \(dq{\(dq, \(dqauthor\(dq, BCON_BOOL (true), \(dq}\(dq); opts = mongoc_find_and_modify_opts_new (); /* Note that the document returned is the _previous_ version of the document * To fetch the modified new version, use * mongoc_find_and_modify_opts_set_flags (opts, * MONGOC_FIND_AND_MODIFY_RETURN_NEW); */ mongoc_find_and_modify_opts_set_update (opts, update); 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); } bson_destroy (&reply); bson_destroy (update); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); } .EE .UNINDENT .UNINDENT .sp fields.c .INDENT 0.0 .INDENT 3.5 .sp .EX void fam_fields (mongoc_collection_t *collection) { mongoc_find_and_modify_opts_t *opts; bson_t fields = BSON_INITIALIZER; bson_t *update; bson_t reply; bson_error_t error; bson_t query = BSON_INITIALIZER; bool success; /* Find Zlatan Ibrahimovic */ BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); BSON_APPEND_UTF8 (&query, \(dqfirstname\(dq, \(dqZlatan\(dq); /* Return his goal tally */ BSON_APPEND_INT32 (&fields, \(dqgoals\(dq, 1); /* Bump his goal tally */ update = BCON_NEW (\(dq$inc\(dq, \(dq{\(dq, \(dqgoals\(dq, BCON_INT32 (1), \(dq}\(dq); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); mongoc_find_and_modify_opts_set_fields (opts, &fields); /* Return the new tally */ mongoc_find_and_modify_opts_set_flags (opts, MONGOC_FIND_AND_MODIFY_RETURN_NEW); 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); } bson_destroy (&reply); bson_destroy (update); bson_destroy (&fields); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); } .EE .UNINDENT .UNINDENT .sp sort.c .INDENT 0.0 .INDENT 3.5 .sp .EX void fam_sort (mongoc_collection_t *collection) { mongoc_find_and_modify_opts_t *opts; bson_t *update; bson_t sort = BSON_INITIALIZER; bson_t reply; bson_error_t error; bson_t query = BSON_INITIALIZER; bool success; /* Find all users with the lastname Ibrahimovic */ BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); /* Sort by age (descending) */ BSON_APPEND_INT32 (&sort, \(dqage\(dq, \-1); /* Bump his goal tally */ update = BCON_NEW (\(dq$set\(dq, \(dq{\(dq, \(dqoldest\(dq, BCON_BOOL (true), \(dq}\(dq); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); mongoc_find_and_modify_opts_set_sort (opts, &sort); 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); } bson_destroy (&reply); bson_destroy (update); bson_destroy (&sort); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); } .EE .UNINDENT .UNINDENT .sp opts.c .INDENT 0.0 .INDENT 3.5 .sp .EX 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, \(dqfirstname\(dq, \(dqZlatan\(dq); BSON_APPEND_UTF8 (&query, \(dqlastname\(dq, \(dqIbrahimovic\(dq); BSON_APPEND_UTF8 (&query, \(dqprofession\(dq, \(dqFootball player\(dq); /* Bump his age */ update = BCON_NEW (\(dq$inc\(dq, \(dq{\(dq, \(dqage\(dq, BCON_INT32 (1), \(dq}\(dq); 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\(aqt support conveniently */ BSON_APPEND_INT32 (&extra, \(dqfutureOption\(dq, 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 (\(dq%s\en\(dq, str); bson_free (str); } else { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __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); } .EE .UNINDENT .UNINDENT .sp fam.c .INDENT 0.0 .INDENT 3.5 .sp .EX int main (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; const char *uri_string = \(dqmongodb://localhost:27017/admin?appname=find\-and\-modify\-opts\-example\(dq; mongoc_uri_t *uri; bson_error_t error; bson_t *options; mongoc_init (); uri = mongoc_uri_new_with_error (uri_string, &error); if (!uri) { fprintf (stderr, \(dqfailed to parse URI: %s\en\(dq \(dqerror message: %s\en\(dq, uri_string, error.message); return EXIT_FAILURE; } client = mongoc_client_new_from_uri (uri); if (!client) { return EXIT_FAILURE; } mongoc_client_set_error_api (client, 2); database = mongoc_client_get_database (client, \(dqdatabaseName\(dq); options = BCON_NEW (\(dqvalidator\(dq, \(dq{\(dq, \(dqage\(dq, \(dq{\(dq, \(dq$lte\(dq, BCON_INT32 (34), \(dq}\(dq, \(dq}\(dq, \(dqvalidationAction\(dq, BCON_UTF8 (\(dqerror\(dq), \(dqvalidationLevel\(dq, BCON_UTF8 (\(dqmoderate\(dq)); collection = mongoc_database_create_collection (database, \(dqcollectionName\(dq, options, &error); if (!collection) { fprintf (stderr, \(dqGot error: \e\(dq%s\e\(dq on line %d\en\(dq, error.message, __LINE__); return EXIT_FAILURE; } fam_flags (collection); fam_bypass (collection); fam_update (collection); fam_fields (collection); fam_opts (collection); fam_sort (collection); mongoc_collection_drop (collection, NULL); bson_destroy (options); mongoc_uri_destroy (uri); mongoc_database_destroy (database); mongoc_collection_destroy (collection); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; } .EE .UNINDENT .UNINDENT .sp Outputs: .INDENT 0.0 .INDENT 3.5 .sp .EX { \(dqlastErrorObject\(dq: { \(dqupdatedExisting\(dq: false, \(dqn\(dq: 1, \(dqupserted\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq } }, \(dqvalue\(dq: { \(dq_id\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq }, \(dqage\(dq: 34, \(dqfirstname\(dq: \(dqZlatan\(dq, \(dqgoals\(dq: 342, \(dqlastname\(dq: \(dqIbrahimovic\(dq, \(dqprofession\(dq: \(dqFootball player\(dq, \(dqposition\(dq: \(dqstriker\(dq }, \(dqok\(dq: 1 } { \(dqlastErrorObject\(dq: { \(dqupdatedExisting\(dq: true, \(dqn\(dq: 1 }, \(dqvalue\(dq: { \(dq_id\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq }, \(dqage\(dq: 34, \(dqfirstname\(dq: \(dqZlatan\(dq, \(dqgoals\(dq: 342, \(dqlastname\(dq: \(dqIbrahimovic\(dq, \(dqprofession\(dq: \(dqFootball player\(dq, \(dqposition\(dq: \(dqstriker\(dq }, \(dqok\(dq: 1 } { \(dqlastErrorObject\(dq: { \(dqupdatedExisting\(dq: true, \(dqn\(dq: 1 }, \(dqvalue\(dq: { \(dq_id\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq }, \(dqage\(dq: 35, \(dqfirstname\(dq: \(dqZlatan\(dq, \(dqgoals\(dq: 342, \(dqlastname\(dq: \(dqIbrahimovic\(dq, \(dqprofession\(dq: \(dqFootball player\(dq, \(dqposition\(dq: \(dqstriker\(dq }, \(dqok\(dq: 1 } { \(dqlastErrorObject\(dq: { \(dqupdatedExisting\(dq: true, \(dqn\(dq: 1 }, \(dqvalue\(dq: { \(dq_id\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq }, \(dqgoals\(dq: 343 }, \(dqok\(dq: 1 } { \(dqlastErrorObject\(dq: { \(dqupdatedExisting\(dq: true, \(dqn\(dq: 1 }, \(dqvalue\(dq: { \(dq_id\(dq: { \(dq$oid\(dq: \(dq56562a99d13e6d86239c7b00\(dq }, \(dqage\(dq: 35, \(dqfirstname\(dq: \(dqZlatan\(dq, \(dqgoals\(dq: 343, \(dqlastname\(dq: \(dqIbrahimovic\(dq, \(dqprofession\(dq: \(dqFootball player\(dq, \(dqposition\(dq: \(dqstriker\(dq, \(dqauthor\(dq: true }, \(dqok\(dq: 1 } .EE .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .