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