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