.\" Man page generated from reStructuredText. . .TH "MONGOC_COLLECTION_FIND_WITH_OPTS" "3" "Jun 04, 2021" "1.17.6" "libmongoc" .SH NAME mongoc_collection_find_with_opts \- mongoc_collection_find_with_opts() . .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 .. .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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) BSON_GNUC_WARN_UNUSED_RESULT; .ft P .fi .UNINDENT .UNINDENT .SH PARAMETERS .INDENT 0.0 .IP \(bu 2 \fBcollection\fP: A \fBmongoc_collection_t\fP\&. .IP \(bu 2 \fBfilter\fP: A \fI\%bson_t\fP containing the query to execute. .IP \(bu 2 \fBopts\fP: A \fI\%bson_t\fP query options, including sort order and which fields to return. Can be \fBNULL\fP\&. .IP \(bu 2 \fBread_prefs\fP: A \fBmongoc_read_prefs_t\fP or \fBNULL\fP\&. .UNINDENT .SH DESCRIPTION .sp Query on \fBcollection\fP, passing arbitrary query options to the server in \fBopts\fP\&. .sp To target a specific server, include an integer "serverId" field in \fBopts\fP with an id obtained first by calling \fBmongoc_client_select_server\fP, then \fBmongoc_server_description_id\fP on its return value. .sp Read preferences, read concern, and collation can be overridden by various sources. In a transaction, read concern and write concern are prohibited in \fBopts\fP and the read preference must be primary or NULL. The highest\-priority sources for these options are listed first in the following table. No write concern is applied. .TS center; |l|l|l|. _ T{ Read Preferences T} T{ Read Concern T} T{ Collation T} _ T{ \fBread_prefs\fP T} T{ \fBopts\fP T} T{ \fBopts\fP T} _ T{ Transaction T} T{ Transaction T} T{ T} _ T{ \fBcollection\fP T} T{ T} T{ T} _ .TE .sp See the example for transactions and for the "distinct" command with opts\&. .sp 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 \fBretryreads\fP is false in the URI (see \fBmongoc_uri_t\fP) the retry behavior does not apply. .SH RETURNS .sp A newly allocated \fBmongoc_cursor_t\fP that must be freed with \fBmongoc_cursor_destroy()\fP\&. .SH EXAMPLES .sp Print First Ten Documents in a Collection .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include #include #include 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\en", str); bson_free (str); } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "An error occurred: %s\en", error.message); } mongoc_cursor_destroy (cursor); bson_destroy (filter); bson_destroy (opts); } .ft P .fi .UNINDENT .UNINDENT .sp More examples of modifying the query with \fBopts\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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); .ft P .fi .UNINDENT .UNINDENT .sp The following options are supported. .TS center; |l|l|l|l|. _ T{ Option T} T{ BSON type T} T{ Option T} T{ BSON type T} _ T{ \fBprojection\fP T} T{ document T} T{ \fBmax\fP T} T{ document T} _ T{ \fBsort\fP T} T{ document T} T{ \fBmaxTimeMS\fP T} T{ non\-negative int64 T} _ T{ \fBskip\fP T} T{ non\-negative int64 T} T{ \fBmaxAwaitTimeMS\fP T} T{ non\-negative int64 T} _ T{ \fBlimit\fP T} T{ non\-negative int64 T} T{ \fBmin\fP T} T{ document T} _ T{ \fBbatchSize\fP T} T{ non\-negative int64 T} T{ \fBnoCursorTimeout\fP T} T{ bool T} _ T{ \fBexhaust\fP T} T{ bool T} T{ \fBoplogReplay\fP T} T{ bool T} _ T{ \fBhint\fP T} T{ string or document T} T{ \fBreadConcern\fP T} T{ document T} _ T{ \fBallowPartialResults\fP T} T{ bool T} T{ \fBreturnKey\fP T} T{ bool T} _ T{ \fBawaitData\fP T} T{ bool T} T{ \fBsessionId\fP T} T{ (none) T} _ T{ \fBcollation\fP T} T{ document T} T{ \fBshowRecordId\fP T} T{ bool T} _ T{ \fBcomment\fP T} T{ string T} T{ \fBsingleBatch\fP T} T{ bool T} _ T{ \fBallowDiskUse\fP T} T{ bool T} T{ T} T{ T} _ .TE .sp All options are documented in the reference page for \fI\%the "find" command\fP in the MongoDB server manual, except for "maxAwaitTimeMS" and "sessionId". .sp "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. .sp To add a "sessionId", construct a \fBmongoc_client_session_t\fP with \fBmongoc_client_start_session\fP\&. You can begin a transaction with \fBmongoc_client_session_start_transaction\fP, optionally with a \fBmongoc_transaction_opt_t\fP that overrides the options inherited from \fBcollection\fP\&. Then use \fBmongoc_client_session_append\fP to add the session to \fBopts\fP\&. See the example code for \fBmongoc_client_session_t\fP\&. .sp To add a "readConcern", construct a \fBmongoc_read_concern_t\fP with \fBmongoc_read_concern_new\fP and configure it with \fBmongoc_read_concern_set_level\fP\&. Then use \fBmongoc_read_concern_append\fP to add the read concern to \fBopts\fP\&. .sp For some options like "collation", the driver returns an error if the server version is too old to support the feature. Any fields in \fBopts\fP that are not listed here are passed to the server unmodified. .sp \fBallowDiskUse\fP is only supported in MongoDB 4.4+. .SH DEPRECATED OPTIONS .sp The \fBsnapshot\fP boolean option is removed in MongoDB 4.0. The \fBmaxScan\fP option, a non\-negative int64, is deprecated in MongoDB 4.0 and will be removed in a future MongoDB version. The \fBoplogReplay\fP boolean option is deprecated in MongoDB 4.4. All of these options are supported by the C Driver with older MongoDB versions. .SH SEE ALSO .sp \fI\%The "find" command\fP in the MongoDB Manual. All options listed there are supported by the C Driver. For MongoDB servers before 3.2, or for exhaust queries, the driver transparently converts the query to a legacy OP_QUERY message. .SH THE "EXPLAIN" COMMAND .sp With MongoDB before 3.2, a query with option \fB$explain: true\fP 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: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C /* 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); .ft P .fi .UNINDENT .UNINDENT .SH SEE ALSO .sp \fI\%The "explain" command\fP in the MongoDB Manual. .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .