1MONGOC_COLLECTION_FIND(3) MongoDB C Driver MONGOC_COLLECTION_FIND(3)
2
3
4
6 mongoc_collection_find - mongoc_collection_find()
7
9 This function is deprecated and should not be used in new code.
10
11 Use the more convenient mongoc_collection_find_with_opts instead.
12
14 mongoc_cursor_t *
15 mongoc_collection_find (mongoc_collection_t *collection,
16 mongoc_query_flags_t flags,
17 uint32_t skip,
18 uint32_t limit,
19 uint32_t batch_size,
20 const bson_t *query,
21 const bson_t *fields,
22 const mongoc_read_prefs_t *read_prefs)
23 BSON_GNUC_DEPRECATED_FOR (mongoc_collection_find_with_opts)
24 BSON_GNUC_WARN_UNUSED_RESULT;
25
27 · collection: A mongoc_collection_t.
28
29 · flags: A mongoc_query_flags_t.
30
31 · skip: A uint32_t of number of documents to skip or 0.
32
33 · limit: A uint32_t of max number of documents to return or 0.
34
35 · batch_size: A uint32_t containing batch size of document result sets
36 or 0 for default. Default is 100.
37
38 · query: A bson_t containing the query and options to execute.
39
40 · fields: A bson_t containing fields to return or NULL.
41
42 · read_prefs: A mongoc_read_prefs_t or NULL for default read prefer‐
43 ences.
44
46 This function shall execute a query on the underlying collection.
47
48 If no options are necessary, query can simply contain a query such as
49 {a:1}. If you would like to specify options such as a sort order, the
50 query must be placed inside of {"$query": {}}. See the example below
51 for how to properly specify additional options to query.
52
54 A newly allocated mongoc_cursor_t that should be freed with mongoc_cur‐
55 sor_destroy() when no longer in use.
56
58 Print All Documents in a Collection.INDENT 0.0
59
60 #include <bson/bson.h>
61 #include <mongoc/mongoc.h>
62 #include <stdio.h>
63
64 static void
65 print_all_documents (mongoc_collection_t *collection)
66 {
67 mongoc_cursor_t *cursor;
68 bson_error_t error;
69 const bson_t *doc;
70 char *str;
71 bson_t *query;
72
73 query = BCON_NEW ("$query",
74 "{",
75 "foo",
76 BCON_INT32 (1),
77 "}",
78 "$orderby",
79 "{",
80 "bar",
81 BCON_INT32 (-1),
82 "}");
83 cursor = mongoc_collection_find (
84 collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
85
86 while (mongoc_cursor_next (cursor, &doc)) {
87 str = bson_as_canonical_extended_json (doc, NULL);
88 printf ("%s\n", str);
89 bson_free (str);
90 }
91
92 if (mongoc_cursor_error (cursor, &error)) {
93 fprintf (stderr, "An error occurred: %s\n", error.message);
94 }
95
96 mongoc_cursor_destroy (cursor);
97 bson_destroy (query);
98 }
99
101 Queries have historically been sent as OP_QUERY wire protocol messages,
102 but beginning in MongoDB 3.2 queries use the "find" command instead.
103
104 The driver automatically converts queries to the new "find" command
105 syntax if needed, so this change is typically invisible to C Driver
106 users. However, an application written exclusively for MongoDB 3.2 and
107 later can choose to use the new syntax directly instead of relying on
108 the driver to convert from the old syntax:
109
110 /* MongoDB 3.2+ "find" command syntax */
111 query = BCON_NEW ("filter",
112 "{",
113 "foo",
114 BCON_INT32 (1),
115 "}",
116 "sort",
117 "{",
118 "bar",
119 BCON_INT32 (-1),
120 "}");
121 cursor = mongoc_collection_find (
122 collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
123
124 The "find" command takes different options from the traditional
125 OP_QUERY message.
126
127 ┌────────────────────┬────────────────┬───────────────┐
128 │Query │ $query │ filter │
129 ├────────────────────┼────────────────┼───────────────┤
130 │Sort │ $orderby │ sort │
131 └────────────────────┴────────────────┴───────────────┘
132
133 │Show record loca‐ │ $showDiskLoc │ showRecordId │
134 │tion │ │ │
135 ├────────────────────┼────────────────┼───────────────┤
136 │Other $-options │ $<option name> │ <option name> │
137 └────────────────────┴────────────────┴───────────────┘
138
139 Most applications should use the OP_QUERY syntax, with "$query",
140 "$orderby", and so on, and rely on the driver to convert to the new
141 syntax if needed.
142
144 The "find" command in the MongoDB Manual.
145
147 With MongoDB before 3.2, a query with option $explain: true returns
148 information about the query plan, instead of the query results. Begin‐
149 ning in MongoDB 3.2, there is a separate "explain" command. The driver
150 will not convert "$explain" queries to "explain" commands, you must
151 call the "explain" command explicitly:
152
153 /* MongoDB 3.2+, "explain" command syntax */
154 command = BCON_NEW ("explain",
155 "{",
156 "find",
157 BCON_UTF8 ("collection_name"),
158 "filter",
159 "{",
160 "foo",
161 BCON_INT32 (1),
162 "}",
163 "}");
164 mongoc_collection_command_simple (collection, command, NULL, &reply, &error);
165
167 The "explain" command in the MongoDB Manual.
168
170 MongoDB, Inc
171
173 2017-present, MongoDB, Inc
174
175
176
177
1781.13.1 Jan 24, 2019 MONGOC_COLLECTION_FIND(3)