1MONGOC_COLLECTION_COUNT_DOCUMENTS(3)libmongoMcONGOC_COLLECTION_COUNT_DOCUMENTS(3)
2
3
4

NAME

6       mongoc_collection_count_documents - mongoc_collection_count_documents()
7

SYNOPSIS

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

PARAMETERS

18collection: A mongoc_collection_t.
19
20filter: A bson_t containing the filter.
21
22opts: A bson_t, NULL to ignore.
23
24read_prefs: A mongoc_read_prefs_t or NULL.
25
26reply:  A  location  for an uninitialized bson_t to store the command
27         reply, NULL to ignore. If not NULL, reply will be initialized.
28
29error: 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
33readConcern:    Construct    a    mongoc_read_concern_t    and    use
34         mongoc_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
38sessionId:    First,   construct   a   mongoc_client_session_t   with
39         mongoc_client_start_session().  You  can  begin  a  transaction  with
40         mongoc_client_session_start_transaction(),    optionally    with    a
41         mongoc_transaction_opt_t that overrides the  options  inherited  from
42         collection, and use mongoc_client_session_append() to add the session
43         to opts. See the example code for mongoc_client_session_t.
44
45collation: Configure textual comparisons. See Setting  Collation  Or‐
46         der,  and  the  MongoDB Manual entry on Collation. Collation requires
47         MongoDB 3.2 or later, otherwise an error is returned.
48
49serverId: 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
53skip: An int specifying how many documents matching the query  should
54         be skipped before counting.
55
56limit: An int specifying the maximum number of documents to count.
57
58comment: A bson_value_t specifying the comment to attach to this com‐
59         mand. The comment will appear in log messages, profiler  output,  and
60         currentOp  output. Specifying a non-string value requires MongoDB 4.4
61         or later.
62

DESCRIPTION

64       This functions executes a count query on collection. In  contrast  with
65       mongoc_collection_estimated_document_count(),  the  count  returned  is
66       guaranteed to be accurate.
67
68       This function is considered a retryable read operation.  Upon  a  tran‐
69       sient error (a network error, errors due to replica set failover, etc.)
70       the operation is safely retried once.  If retryreads is  false  in  the
71       URI (see mongoc_uri_t) the retry behavior does not apply.
72

ERRORS

74       Errors are propagated via the error parameter.
75

RETURNS

77       -1 on failure, otherwise the number of documents counted.
78

EXAMPLE

80          #include <bson/bson.h>
81          #include <mongoc/mongoc.h>
82          #include <stdio.h>
83
84          static void
85          print_count (mongoc_collection_t *collection, bson_t *filter)
86          {
87             bson_error_t error;
88             int64_t count;
89             bson_t* opts = BCON_NEW ("skip", BCON_INT64(5));
90
91             count = mongoc_collection_count_documents (
92                collection, filter, opts, NULL, NULL, &error);
93             bson_destroy (opts);
94
95             if (count < 0) {
96                fprintf (stderr, "Count failed: %s\n", error.message);
97             } else {
98                printf ("%" PRId64 " documents counted.\n", count);
99             }
100          }
101

MIGRATING FROM DEPRECATED COUNT FUNCTIONS

103       When  migrating  to mongoc_collection_count_documents() from the depre‐
104       cated mongoc_collection_count() or mongoc_collection_count_with_opts(),
105       the following query operators in the filter must be replaced:
106
107                     ┌────────────┬────────────────────────────┐
108                     │Operator    │ Replacement                │
109                     ├────────────┼────────────────────────────┤
110                     │$where      │ $expr
111                     ├────────────┼────────────────────────────┤
112                     │$near       │ $geoWithin with $center
113                     ├────────────┼────────────────────────────┤
114                     │$nearSphere │ $geoWithin            with │
115                     │            │ $centerSphere
116                     └────────────┴────────────────────────────┘
117
118       $expr requires MongoDB 3.6+
119
120       SEE ALSO:
121          mongoc_collection_estimated_document_count()
122
123

AUTHOR

125       MongoDB, Inc
126
128       2017-present, MongoDB, Inc
129
130
131
132
1331.23.1                           Oct 20, 20M2O2NGOC_COLLECTION_COUNT_DOCUMENTS(3)
Impressum