MONGOC_FIND_AN..._OPTS_SET_SORT(3)

πŸ’… MONGOC_FIND_AND_MODIFY_OPTS_SET_SORT β€” libmongoc, bestie edition

okay so you're in the libmongoc docs and you clicked on mongoc_find_and_modify_opts_set_sort β€” that's the whole name, the header just truncates it because whoever laid out this manpage was being dramatic πŸ’€. Anyway. This is the little function that tells your findAndModify builder which document to grab when your query matches, like, way too many of them. Let's get into it ✨


✨ SYNOPSIS ✨

bool
mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts,
                                      const bson_t *sort);

that's it, that's the signature, she's short and iconic πŸŽ€


🌸 PARAMETERS 🌸

  • opts β€” a mongoc_find_and_modify_opts_t. your builder object, your main character, the thing you're stuffing options into 🫢
  • sort β€” this is the one that decides which document the operation modifies if the query selects multiple documents. like, findAndModify literally modifies the first document in the sort order specified by this argument. so if your query is catching like ten docs, sort is what picks the winner πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

it adds the sort argument to the builder. that's the assignment, no cap.

and here's the nice part: sort does not have to remain valid after you call this function. so you can yeet it, destroy it, whatever β€” the builder already took what it needed πŸ’…


⭐ RETURNS ⭐

returns true if it successfully added the option to the builder, otherwise false. simple as that, no notes πŸ’–


πŸ“ SETTING SORT πŸ“

this is the example, from sort.c, and it's honestly a slay. read it, bestie:

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, "lastname", "Ibrahimovic");

   /* Sort by age (descending) */
   BSON_APPEND_INT32(&sort, "age", -1);

   /* Bump his goal tally */
   update = BCON_NEW("$set", "{", "oldest", BCON_BOOL(true), "}");

   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("%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(update);
   bson_destroy(&sort);
   bson_destroy(&query);
   mongoc_find_and_modify_opts_destroy(opts);
}

🌈 Outputs 🌈

and this is what she prints, verbatim, weird indentation and all (we respect the original formatting here, it's part of the vibe):

{
   "lastErrorObject" : {"updatedExisting" : true, "n" : 1},
                       "value" : {
                          "_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
                          "age" : 35,
                          "firstname" : "Zlatan",
                          "goals" : 343,
                          "lastname" : "Ibrahimovic",
                          "profession" : "Football player",
                          "position" : "striker",
                          "author" : true
                       },
                                 "ok" : 1
}

πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc β€” credited exactly like that, we're not editing the receipts 🧚


2009-present, MongoDB, Inc. β€” periodt.


ok that's the whole page!! every flag, every field, every line of that example made it through, promise πŸ™ˆ don't come for me if you skimmed the code block, go back and actually read it, iykyk πŸ’€

and before you close the tab: this whole thing is from libmongoc, version 2.5.6, dated Oct 09, 202… (the footer got cut off in the middle of the year, iconic behaviour honestly β€” that's genuinely how it shows up, I'm not gonna invent a digit for you 🌸). mwah, go write some queries bestie πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from