MONGOC_FIND_AND_MODIFY_OPTS_SET_FLAGS(3)

NAME
mongoc_find_and_modify_opts_set_flags - mongoc_find_and_modify_opts_set_flags()

SYNOPSIS

bool
mongoc_find_and_modify_opts_set_flags (
   mongoc_find_and_modify_opts_t *opts,
   const mongoc_find_and_modify_flags_t flags);

PARAMETERS

•   opts: A mongoc_find_and_modify_opts_t.

•   flags: .

DESCRIPTION

Adds one or more flags to the builder.
MONGOC_FIND_AND_MODIFY_NONE | Default. Doesn't add anything to the builder.
MONGOC_FIND_AND_MODIFY_REMOVE | Will instruct find_and_modify to remove the matching document.
MONGOC_FIND_AND_MODIFY_UPSERT | Update the matching document or, if no document matches, insert the document.
MONGOC_FIND_AND_MODIFY_RETURN_NEW | Return the resulting document.

RETURNS

Returns Returns true if it successfully added the option to the builder, otherwise false and logs an error.

SETTING FLAGS

flags.c

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, "firstname", "Zlatan");
   BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
   BSON_APPEND_UTF8 (&query, "profession", "Football player");
   BSON_APPEND_INT32 (&query, "age", 34);
   BSON_APPEND_INT32 (
      &query, "goals", (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62));
   /* Add his football position */
   update = BCON_NEW ("$set", "{", "position", BCON_UTF8 ("striker"), "}");
   opts = mongoc_find_and_modify_opts_new ();
   mongoc_find_and_modify_opts_set_update (opts, update);
   /* Create the document if it didn't 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 ("%s\n", str);
      bson_free (str);
   } else {
      fprintf (
         stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
   }
   bson_destroy (&reply);
   bson_destroy (update);
   bson_destroy (&query);
   mongoc_find_and_modify_opts_destroy (opts);
}

Outputs:

{
   "lastErrorObject" : {
      "updatedExisting" : false,
      "n" : 1,
      "upserted" : {"$oid" : "56562a99d13e6d86239c7b00"}
   },
                       "value" : {
                          "_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
                          "age" : 34,
                          "firstname" : "Zlatan",
                          "goals" : 342,
                          "lastname" : "Ibrahimovic",
                          "profession" : "Football player",
                          "position" : "striker"
                       },
                                 "ok" : 1
}

AUTHOR
MongoDB, Inc

COPYRIGHT
2017-present, MongoDB, Inc