PARAMETERS

A uint32_t containing batch size of document result sets or 0 for default. Default is 100.

query

A
bson_t
containing the query and options to execute.

fields

A
bson_t
containing fields to return or
NULL
\&.

read_prefs

A
mongoc_read_prefs_t
or
NULL
for default read preferences.

DESCRIPTION

This function shall execute a query on the underlying
collection
\&.

If no options are necessary,
query
can simply contain a query such as
{a:1}
\&. If you would like to specify options such as a sort order, the query must be placed inside of
{$query: {}}
as specified by the server documentation. See the example below for how to properly specify additional options to
query
\&.

RETURNS

A newly allocated
mongoc_cursor_t
that should be freed with
mongoc_cursor_destroy(3)
when no longer in use. If invalid parameters are supplied,
NULL
may be returned.

THE FIND COMMAND

Queries have historically been sent as OP_QUERY wire protocol messages, but beginning in MongoDB 3.2 queries use
the find command
instead.

The driver automatically converts queries to the new "find" command syntax if needed, so this change is typically invisible to C Driver users. However, an application written exclusively for MongoDB 3.2 and later can choose to use the new syntax directly instead of relying on the driver to convert from the old syntax:

The "find" command takes different options from the traditional OP_QUERY message.

Query

$queryfilter

Sort

$orderbysort

Show record location

$showDiskLocshowRecordId

Other $-options

$<option name><option name>

Most applications should use the OP_QUERY syntax, with "$query", "$orderby", and so on, and rely on the driver to convert to the new syntax if needed. There are two caveats: querying documents by a key named "filter", and using new "find" command options that OP_QUERY does not support.

FINDING A DOCUMENT BY A KEY NAMED FILTER

To find a document like
{ _id: 1, filter: value }
, this query works in MongoDB before 3.2:

Beginning in MongoDB 3.2, the "filter" option has special meaning, and it is no longer assumed to be a field in a document you are querying for. To execute this query on any MongoDB version, wrap it in "$query":

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: