1MONGOC_COLLECTION_COUNT_WITH_OPTS(3)libmongoMcONGOC_COLLECTION_COUNT_WITH_OPTS(3)
2
3
4
6 mongoc_collection_count_with_opts - mongoc_collection_count_with_opts()
7
9 This function is deprecated and should not be used in new code. Use
10 mongoc_collection_count_documents() or
11 mongoc_collection_estimated_document_count() instead.
12
13 mongoc_collection_count_documents() has similar performance to calling
14 mongoc_collection_count() with a non-NULL query, and is guaranteed to
15 retrieve an accurate collection count. See migrating from deprecated
16 count functions for details.
17
18 mongoc_collection_estimated_document_count() has the same performance
19 as calling mongoc_collection_count() with a NULL query, but is not
20 guaranteed to retrieve an accurate collection count.
21
23 int64_t
24 mongoc_collection_count_with_opts (mongoc_collection_t *collection,
25 mongoc_query_flags_t flags,
26 const bson_t *query,
27 int64_t skip,
28 int64_t limit,
29 const bson_t *opts,
30 const mongoc_read_prefs_t *read_prefs,
31 bson_error_t *error)
32 BSON_GNUC_DEPRECATED_FOR (mongoc_collection_count_documents or
33 mongoc_collection_estimated_document_count);
34
36 • collection: A mongoc_collection_t.
37
38 • flags: A mongoc_query_flags_t.
39
40 • query: A bson_t containing the query.
41
42 • skip: A int64_t, zero to ignore.
43
44 • limit: A int64_t, zero to ignore.
45
46 • opts: A bson_t, NULL to ignore.
47
48 • read_prefs: An optional mongoc_read_prefs_t, otherwise uses the col‐
49 lection's read preference.
50
51 • error: An optional location for a bson_error_t or NULL.
52
53 opts may be NULL or a BSON document with additional command options:
54
55 • readConcern: Construct a mongoc_read_concern_t and use
56 mongoc_read_concern_append() to add the read concern to opts. See the
57 example code for mongoc_client_read_command_with_opts(). Read concern
58 requires MongoDB 3.2 or later, otherwise an error is returned.
59
60 • sessionId: First, construct a mongoc_client_session_t with
61 mongoc_client_start_session(). You can begin a transaction with
62 mongoc_client_session_start_transaction(), optionally with a
63 mongoc_transaction_opt_t that overrides the options inherited from
64 collection, and use mongoc_client_session_append() to add the session
65 to opts. See the example code for mongoc_client_session_t.
66
67 • collation: Configure textual comparisons. See Setting Collation Or‐
68 der, and the MongoDB Manual entry on Collation. Collation requires
69 MongoDB 3.2 or later, otherwise an error is returned.
70
71 • serverId: To target a specific server, include an int32 "serverId"
72 field. Obtain the id by calling mongoc_client_select_server(), then
73 mongoc_server_description_id() on its return value.
74
76 This function shall execute a count query on the underlying 'collec‐
77 tion'. The bson 'query' is not validated, simply passed along as appro‐
78 priate to the server. As such, compatibility and errors should be val‐
79 idated in the appropriate server documentation.
80
81 The mongoc_read_concern_t specified on the mongoc_collection_t will be
82 used, if any. If read_prefs is NULL, the collection's read preferences
83 are used.
84
85 In addition to the standard functionality available from mongoc_collec‐
86 tion_count, this function allows the user to add arbitrary extra keys
87 to the count. This pass through enables features such as hinting for
88 counts.
89
90 For more information, see the query reference at the MongoDB website.
91
92 This function is considered a retryable read operation. Upon a tran‐
93 sient error (a network error, errors due to replica set failover, etc.)
94 the operation is safely retried once. If retryreads is false in the
95 URI (see mongoc_uri_t) the retry behavior does not apply.
96
98 Errors are propagated via the error parameter.
99
101 -1 on failure, otherwise the number of documents counted.
102
104 Basic Counting
105
106 #include <bson/bson.h>
107 #include <mongoc/mongoc.h>
108 #include <stdio.h>
109
110 static void
111 print_query_count (mongoc_collection_t *collection, bson_t *query)
112 {
113 bson_error_t error;
114 int64_t count;
115 bson_t opts;
116
117 bson_init (&opts);
118 BSON_APPEND_UTF8 (&opts, "hint", "_id_");
119
120 count = mongoc_collection_count_with_opts (
121 collection, MONGOC_QUERY_NONE, query, 0, 0, &opts, NULL, &error);
122
123 bson_destroy (&opts);
124
125 if (count < 0) {
126 fprintf (stderr, "Count failed: %s\n", error.message);
127 } else {
128 printf ("%" PRId64 " documents counted.\n", count);
129 }
130 }
131
132 Counting with Collation
133
134 #include <bson/bson.h>
135 #include <mongoc/mongoc.h>
136 #include <stdio.h>
137
138 static void
139 print_query_count (mongoc_collection_t *collection, bson_t *query)
140 {
141 bson_t *selector;
142 bson_t *opts;
143 bson_error_t error;
144 int64_t count;
145
146 selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
147
148 /* "One" normally sorts before "one"; make "one" come first */
149 opts = BCON_NEW ("collation",
150 "{",
151 "locale",
152 BCON_UTF8 ("en_US"),
153 "caseFirst",
154 BCON_UTF8 ("lower"),
155 "}");
156
157 count = mongoc_collection_count_with_opts (
158 collection, MONGOC_QUERY_NONE, query, 0, 0, opts, NULL, &error);
159
160 bson_destroy (selector);
161 bson_destroy (opts);
162
163 if (count < 0) {
164 fprintf (stderr, "Count failed: %s\n", error.message);
165 } else {
166 printf ("%" PRId64 " documents counted.\n", count);
167 }
168 }
169
171 MongoDB, Inc
172
174 2017-present, MongoDB, Inc
175
176
177
178
1791.23.1 Oct 20, 20M2O2NGOC_COLLECTION_COUNT_WITH_OPTS(3)