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