1MONGOC_COLLECTION_COUNT_DOCUMENTMSo(n3g)oDB C DrMiOvNeGrOC_COLLECTION_COUNT_DOCUMENTS(3)
2
3
4
6 mongoc_collection_count_documents - mongoc_collection_count_documents()
7
9 int64_t
10 mongoc_collection_count_documents (mongoc_collection_t *collection,
11 const bson_t *filter,
12 const bson_t *opts,
13 const mongoc_read_prefs_t *read_prefs,
14 bson_t *reply,
15 bson_error_t *error);
16
18 · collection: A mongoc_collection_t.
19
20 · filter: A bson_t containing the filter.
21
22 · opts: A bson_t, NULL to ignore.
23
24 · read_prefs: A mongoc_read_prefs_t or NULL.
25
26 · reply: A location for an uninitialized bson_t to store the command
27 reply, NULL to ignore. If not NULL, reply will be initialized.
28
29 · error: An optional location for a bson_error_t or NULL.
30
31 opts may be NULL or a BSON document with additional command options:
32
33 · readConcern: Construct a mongoc_read_concern_t and use mon‐
34 goc_read_concern_append to add the read concern to opts. See the
35 example code for mongoc_client_read_command_with_opts. Read concern
36 requires MongoDB 3.2 or later, otherwise an error is returned.
37
38 · sessionId: First, construct a mongoc_client_session_t with mon‐
39 goc_client_start_session. You can begin a transaction with mon‐
40 goc_client_session_start_transaction, optionally with a mongoc_trans‐
41 action_opt_t that overrides the options inherited from collection,
42 and use mongoc_client_session_append to add the session to opts. See
43 the example code for mongoc_client_session_t.
44
45 · collation: Configure textual comparisons. See Setting Collation
46 Order, and the MongoDB Manual entry on Collation. Collation requires
47 MongoDB 3.2 or later, otherwise an error is returned.
48
49 · serverId: To target a specific server, include an int32 "serverId"
50 field. Obtain the id by calling mongoc_client_select_server, then
51 mongoc_server_description_id on its return value.
52
53 · skip: An int specifying how many documents matching the query should
54 be skipped before counting.
55
56 · limit: An int specifying the maximum number of documents to count.
57
59 This functions executes a count query on collection. In contrast with
60 mongoc_collection_estimated_document_count(), the count returned is
61 guaranteed to be accurate.
62
64 Errors are propagated via the error parameter.
65
67 -1 on failure, otherwise the number of documents counted.
68
70 #include <bson/bson.h>
71 #include <mongoc/mongoc.h>
72 #include <stdio.h>
73
74 static void
75 print_count (mongoc_collection_t *collection, bson_t *filter)
76 {
77 bson_error_t error;
78 int64_t count;
79 bson_t* opts = BCON_NEW ("skip", BCON_INT64(5));
80
81 count = mongoc_collection_count_documents (
82 collection, filter, opts, NULL, NULL, &error);
83 bson_destroy (opts);
84
85 if (count < 0) {
86 fprintf (stderr, "Count failed: %s\n", error.message);
87 } else {
88 printf ("%" PRId64 " documents counted.\n", count);
89 }
90 }
91
93 When migrating to mongoc_collection_count_documents from the deprecated
94 mongoc_collection_count or mongoc_collection_count_with_opts, the fol‐
95 lowing query operators in the filter must be replaced:
96
97 ┌────────────┬────────────────────────────┐
98 │Operator │ Replacement │
99 ├────────────┼────────────────────────────┤
100 │$where │ $expr │
101 ├────────────┼────────────────────────────┤
102 │$near │ $geoWithin with $center │
103 ├────────────┼────────────────────────────┤
104 │$nearSphere │ $geoWithin with │
105 │ │ $centerSphere │
106 └────────────┴────────────────────────────┘
107
108 $expr requires MongoDB 3.6+
109
111 mongoc_collection_estimated_document_count()
112
114 MongoDB, Inc
115
117 2017-present, MongoDB, Inc
118
119
120
121
1221.14.0 Feb 22, 20M1O9NGOC_COLLECTION_COUNT_DOCUMENTS(3)