MONGOC_COLLECT...FIND_WITH_OPTS(3)
okay bestie, this is MONGOC_COLLECT...FIND_WITH_OPTS(3) from libmongoc β the manual page, but make it make sense π
β¨ SYNOPSIS β¨
mongoc_cursor_t *
mongoc_collection_find_with_opts (mongoc_collection_t *collection,
const bson_t *filter,
const bson_t *opts,
const mongoc_read_prefs_t *read_prefs);
π«Ά PARAMETERS π«Ά
collectionβ amongoc_collection_t. That's the collection we're querying, the main character of this whole situation.filterβ abson_tcontaining the query to execute. Like, the actual "what are we even looking for" of it all.optsβ abson_tof query options, including sort order and which fields to return. Can beNULL.read_prefsβ amongoc_read_prefs_torNULL.
πΈ DESCRIPTION πΈ
mongoc_collection_find_with_opts() queries on collection, passing arbitrary query options to the server in opts. Lowkey the whole vibe is "you bring the filter, I bring the options" β¨.
To target a specific server, include an integer "serverId" field in opts with an id obtained first by calling mongoc_client_select_server(), then mongoc_server_description_id() on its return value. That's the little treasure map for picking your server πΊοΈ.
Read preferences, read concern, and collation can be overridden by various sources. In a transaction, read concern and write concern are prohibited in opts and the read preference must be primary or NULL. The highest-priority sources for these options are listed first β here's the table's whole deal, translated into list form:
Read Preferences (highest priority first):
read_prefs- Transaction
- collection
Read Concern (highest priority first):
opts- Transaction
- (blank in the original table)
Collation (highest priority first):
opts- (blank in the original table)
- (blank in the original table)
No write concern is applied. Periodt. π
See the example for transactions and for the "distinct" command with opts π.
This function is considered a retryable read operation π. Upon a transient error (a network error, errors due to replica set failover, etc.) the operation is safely retried once. If retryreads is false in the URI (see mongoc_uri_t) the retry behavior does not apply.
π RETURNS π
This function returns a newly allocated mongoc_cursor_t that should be freed with mongoc_cursor_destroy() when no longer in use. The returned mongoc_cursor_t is never NULL, even on error. The user must call mongoc_cursor_next() on the returned mongoc_cursor_t to execute the initial command.
Cursor errors can be checked with mongoc_cursor_error_document(). It always fills out the bson_error_t if an error occurred, and optionally includes a server reply document if the error occurred server-side.
π WARNING π
Failure to handle the result of this function is a programming error.
β¨ EXAMPLES β¨
Okay bestie, let's actually use this thing. π
π¨οΈ Print First Ten Documents in a Collection
Here's the whole little program vibe:
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>
static void
print_ten_documents (mongoc_collection_t *collection)
{
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
bson_error_t error;
const bson_t *doc;
char *str;
/* filter by "foo": 1, order by "bar" descending */
filter = BCON_NEW ("foo", BCON_INT32 (1));
opts = BCON_NEW (
"limit", BCON_INT64 (10), "sort", "{", "bar", BCON_INT32 (-1), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
if (mongoc_cursor_error (cursor, &error)) {
fprintf (stderr, "An error occurred: %s\n", error.message);
}
mongoc_cursor_destroy (cursor);
bson_destroy (filter);
bson_destroy (opts);
}
More examples of modifying the query with opts:
bson_t *filter;
bson_t *opts;
mongoc_read_prefs_t *read_prefs;
filter = BCON_NEW ("foo", BCON_INT32 (1));
/* Include "field_name_one" and "field_name_two" in "projection", omit
* others. "_id" must be specifically removed or it is included by default.
*/
opts = BCON_NEW ("projection", "{",
"field_name_one", BCON_BOOL (true),
"field_name_two", BCON_BOOL (true),
"_id", BCON_BOOL (false),
"}",
"tailable", BCON_BOOL (true),
"awaitData", BCON_BOOL (true),
"sort", "{", "bar", BCON_INT32 (-1), "}",
"collation", "{",
"locale", BCON_UTF8("en_US"),
"caseFirst", BCON_UTF8 ("lower"),
"}");
read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
cursor =
mongoc_collection_find_with_opts (collection, filter, opts, read_prefs);
The following options are supported. π
projectionβdocumentmaxβdocumentsortβdocumentmaxTimeMSβnon-negative int64skipβnon-negative int64maxAwaitTimeMSβnon-negative int64limitβnon-negative int64minβdocumentbatchSizeβnon-negative int64noCursorTimeoutβboolexhaustβbooloplogReplayβboolhintβstring or documentreadConcernβdocumentallowPartialResultsβboolreturnKeyβboolawaitDataβboolsessionIdβ(none)collationβdocumentshowRecordIdβboolcommentβanysingleBatchβboolallowDiskUseβboolletβdocument
All options are documented in the reference page for the "find" command in the MongoDB server manual, except for "maxAwaitTimeMS", "sessionId", and "exhaust".
"maxAwaitTimeMS" is the maximum amount of time for the server to wait on new documents to satisfy a query, if "tailable" and "awaitData" are both true. If no new documents are found, the tailable cursor receives an empty batch. The "maxAwaitTimeMS" option is ignored for MongoDB older than 3.4.
To add a "sessionId", 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. Then use mongoc_client_session_append() to add the session to opts. See the example code for mongoc_client_session_t.
To add a "readConcern", construct a mongoc_read_concern_t with mongoc_read_concern_new() and configure it with mongoc_read_concern_set_level(). Then use mongoc_read_concern_append() to add the read concern to opts.
"exhaust" requests the construction of an exhaust cursor.
For some options like "collation", the driver returns an error if the server version is too old to support the feature. Any fields in opts that are not listed here are passed to the server unmodified.
allowDiskUse is only supported in MongoDB 4.4+.
comment only supports string values prior to MongoDB 4.4.
SEE ALSO: The "find" command in the MongoDB Manual. All options listed there are supported by the C Driver. For MongoDB servers before 3.2, the driver transparently converts the query to a legacy OP_QUERY message.
π DEPRECATED OPTIONS π
The snapshot boolean option is removed in MongoDB 4.0. The maxScan option, a non-negative int64, is deprecated in MongoDB 4.0 and will be removed in a future MongoDB version. The oplogReplay boolean option is deprecated in MongoDB 4.4. All of these options are supported by the C Driver with older MongoDB versions.
π THE EXPLAIN COMMAND π
With MongoDB before 3.2, a query with option $explain: true returns information about the query plan, instead of the query results. Beginning in MongoDB 3.2, there is a separate "explain" command. The driver will not convert "$explain" queries to "explain" commands, you must call the "explain" command explicitly:
/* MongoDB 3.2+, "explain" command syntax */
command = BCON_NEW ("explain", "{",
"find", BCON_UTF8 ("collection_name"),
"filter", "{", "foo", BCON_INT32 (1), "}",
"}");
mongoc_collection_command_simple (collection, command, NULL, &reply, &error);
SEE ALSO: The "explain" command in the MongoDB Manual.
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
That's the whole page, bestie! Footer energy: version 2.5.6, dated Oct 09, 202β¦ β the date is literally cut off in the manpage, but we still stan. π«Άβ¨
π¦ 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