1MONGOC_COLLECTION_COUNT_WITH_OPTMSo(n3g)oDB C DrMiOvNeGrOC_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 mongoc_collection_estimated_docu‐
11 ment_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 as
19 calling mongoc_collection_count with a NULL query, but is not guaran‐
20 teed 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 mon‐
56 goc_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 mon‐
61 goc_client_start_session. You can begin a transaction with mon‐
62 goc_client_session_start_transaction, optionally with a mongoc_trans‐
63 action_opt_t that overrides the options inherited from collection,
64 and use mongoc_client_session_append to add the session to opts. See
65 the example code for mongoc_client_session_t.
66
67 · collation: Configure textual comparisons. See Setting Collation
68 Order, 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
93 Errors are propagated via the error parameter.
94
96 -1 on failure, otherwise the number of documents counted.
97
99 Basic Counting.INDENT 0.0
100
101 #include <bson/bson.h>
102 #include <mongoc/mongoc.h>
103 #include <stdio.h>
104
105 static void
106 print_query_count (mongoc_collection_t *collection, bson_t *query)
107 {
108 bson_error_t error;
109 int64_t count;
110 bson_t opts;
111
112 bson_init (&opts);
113 BSON_APPEND_UTF8 (&opts, "hint", "_id_");
114
115 count = mongoc_collection_count_with_opts (
116 collection, MONGOC_QUERY_NONE, query, 0, 0, &opts, NULL, &error);
117
118 bson_destroy (&opts);
119
120 if (count < 0) {
121 fprintf (stderr, "Count failed: %s\n", error.message);
122 } else {
123 printf ("%" PRId64 " documents counted.\n", count);
124 }
125 }
126Counting with Collation.INDENT 0.0
127
128 #include <bson/bson.h>
129 #include <mongoc/mongoc.h>
130 #include <stdio.h>
131
132 static void
133 print_query_count (mongoc_collection_t *collection, bson_t *query)
134 {
135 bson_t *selector;
136 bson_t *opts;
137 bson_error_t error;
138 int64_t count;
139
140 selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
141
142 /* "One" normally sorts before "one"; make "one" come first */
143 opts = BCON_NEW ("collation",
144 "{",
145 "locale",
146 BCON_UTF8 ("en_US"),
147 "caseFirst",
148 BCON_UTF8 ("lower"),
149 "}");
150
151 count = mongoc_collection_count_with_opts (
152 collection, MONGOC_QUERY_NONE, query, 0, 0, opts, NULL, &error);
153
154 bson_destroy (selector);
155 bson_destroy (opts);
156
157 if (count < 0) {
158 fprintf (stderr, "Count failed: %s\n", error.message);
159 } else {
160 printf ("%" PRId64 " documents counted.\n", count);
161 }
162 }
163
165 MongoDB, Inc
166
168 2017-present, MongoDB, Inc
169
170
171
172
1731.13.1 Jan 24, 20M1O9NGOC_COLLECTION_COUNT_WITH_OPTS(3)