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