1MONGOC_COLLECTION_COUNT(3) libmongoc MONGOC_COLLECTION_COUNT(3)
2
3
4
6 mongoc_collection_count - mongoc_collection_count()
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
24 This function is considered a retryable read operation. Upon a tran‐
25 sient error (a network error, errors due to replica set failover, etc.)
26 the operation is safely retried once. If retryreads is false in the
27 URI (see mongoc_uri_t) the retry behavior does not apply.
28
30 int64_t
31 mongoc_collection_count (mongoc_collection_t *collection,
32 mongoc_query_flags_t flags,
33 const bson_t *query,
34 int64_t skip,
35 int64_t limit,
36 const mongoc_read_prefs_t *read_prefs,
37 bson_error_t *error)
38 BSON_GNUC_DEPRECATED_FOR (mongoc_collection_count_documents or
39 mongoc_collection_estimated_document_count);
40
42 • collection: A mongoc_collection_t.
43
44 • flags: A mongoc_query_flags_t.
45
46 • query: A bson_t containing the query.
47
48 • skip: A int64_t, zero to ignore.
49
50 • limit: A int64_t, zero to ignore.
51
52 • read_prefs: A mongoc_read_prefs_t or NULL.
53
54 • error: An optional location for a bson_error_t or NULL.
55
57 This function shall execute a count query on the underlying 'collec‐
58 tion'. The bson 'query' is not validated, simply passed along as appro‐
59 priate to the server. As such, compatibility and errors should be val‐
60 idated in the appropriate server documentation.
61
62 For more information, see the query reference at the MongoDB website.
63
64 The mongoc_read_concern_t specified on the mongoc_collection_t will be
65 used, if any. If read_prefs is NULL, the collection's read preferences
66 are used.
67
69 Errors are propagated via the error parameter.
70
72 -1 on failure, otherwise the number of documents counted.
73
75 #include <bson/bson.h>
76 #include <mongoc/mongoc.h>
77 #include <stdio.h>
78
79 static void
80 print_query_count (mongoc_collection_t *collection, bson_t *query)
81 {
82 bson_error_t error;
83 int64_t count;
84
85 count = mongoc_collection_count (
86 collection, MONGOC_QUERY_NONE, query, 0, 0, NULL, &error);
87
88 if (count < 0) {
89 fprintf (stderr, "Count failed: %s\n", error.message);
90 } else {
91 printf ("%" PRId64 " documents counted.\n", count);
92 }
93 }
94
96 MongoDB, Inc
97
99 2017-present, MongoDB, Inc
100
101
102
103
1041.24.3 Aug 17, 2023 MONGOC_COLLECTION_COUNT(3)