1MONGOC_COLLECTION_ESTIMATED_DOCUMEMNOTlN_iGCbOOmCUo_NnCTgO(oL3cL)ECTION_ESTIMATED_DOCUMENT_COUNT(3)
2
3
4
6 int64_t
7 mongoc_collection_estimated_document_count (mongoc_collection_t *collection,
8 const bson_t *opts,
9 const mongoc_read_prefs_t *read_prefs,
10 bson_t *reply,
11 bson_error_t *error);
12
14 • collection: A mongoc_collection_t.
15
16 • opts: A bson_t, NULL to ignore.
17
18 • read_prefs: A mongoc_read_prefs_t or NULL.
19
20 • reply: A location for an uninitialized bson_t to store the command
21 reply, NULL to ignore. If not NULL, reply will be initialized.
22
23 • error: An optional location for a bson_error_t or NULL.
24
25 opts may be NULL or a BSON document with additional command options:
26
27 • readConcern: Construct a mongoc_read_concern_t and use
28 mongoc_read_concern_append() to add the read concern to opts. See the
29 example code for mongoc_client_read_command_with_opts(). Read concern
30 requires MongoDB 3.2 or later, otherwise an error is returned.
31
32 • sessionId: First, construct a mongoc_client_session_t with
33 mongoc_client_start_session(). You can begin a transaction with
34 mongoc_client_session_start_transaction(), optionally with a
35 mongoc_transaction_opt_t that overrides the options inherited from
36 collection, and use mongoc_client_session_append() to add the session
37 to opts. See the example code for mongoc_client_session_t.
38
39 • collation: Configure textual comparisons. See Setting Collation Or‐
40 der, and the MongoDB Manual entry on Collation. Collation requires
41 MongoDB 3.2 or later, otherwise an error is returned.
42
43 • serverId: To target a specific server, include an int32 "serverId"
44 field. Obtain the id by calling mongoc_client_select_server(), then
45 mongoc_server_description_id() on its return value.
46
47 • skip: An int specifying how many documents matching the query should
48 be skipped before counting.
49
50 • limit: An int specifying the maximum number of documents to count.
51
52 • comment: A bson_value_t specifying the comment to attach to this com‐
53 mand. The comment will appear in log messages, profiler output, and
54 currentOp output. Requires MongoDB 4.4 or later.
55
56 For a list of all options, see the MongoDB Manual entry on the count
57 command.
58
60 This functions executes a count query on collection. In contrast with
61 mongoc_collection_count_documents(), the count returned is not guaran‐
62 teed to be accurate.
63
64 This function is considered a retryable read operation. Upon a tran‐
65 sient error (a network error, errors due to replica set failover, etc.)
66 the operation is safely retried once. If retryreads is false in the
67 URI (see mongoc_uri_t) the retry behavior does not apply.
68
69 Behavior
70 This method is implemented using the count command. Due to an oversight
71 in versions 5.0.0-5.0.8 of MongoDB, the count command was not included
72 in version "1" of the Stable API. Applications using this method with
73 the Stable API are recommended to upgrade their server version to
74 5.0.9+ or disable strict mode (via :symbol:`mongoc_server_api_strict())
75 to avoid encountering errors.
76
78 Errors are propagated via the error parameter.
79
81 -1 on failure, otherwise the number of documents counted.
82
84 #include <bson/bson.h>
85 #include <mongoc/mongoc.h>
86 #include <stdio.h>
87
88 static void
89 print_count (mongoc_collection_t *collection, bson_t *query)
90 {
91 bson_error_t error;
92 int64_t count;
93 bson_t* opts = BCON_NEW ("skip", BCON_INT64(5));
94
95 count = mongoc_collection_estimated_document_count (
96 collection, opts, NULL, NULL, &error);
97 bson_destroy (opts);
98
99 if (count < 0) {
100 fprintf (stderr, "Count failed: %s\n", error.message);
101 } else {
102 printf ("%" PRId64 " documents counted.\n", count);
103 }
104 }
105
106 SEE ALSO:
107 mongoc_collection_count_documents()
108 Count: Behavior in the MongoDB Manual
109
110
112 MongoDB, Inc
113
115 2017-present, MongoDB, Inc
116
117
118
119
1201.25.1 NMoOvNG0O8C,_C2O0L2L3ECTION_ESTIMATED_DOCUMENT_COUNT(3)