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   mon‐
34         goc_read_concern_append  to add the read concern to opts. See the ex‐
35         ample code for mongoc_client_read_command_with_opts. Read concern re‐
36         quires MongoDB 3.2 or later, otherwise an error is returned.
37
38sessionId:  First,  construct  a  mongoc_client_session_t  with  mon‐
39         goc_client_start_session. You  can  begin  a  transaction  with  mon‐
40         goc_client_session_start_transaction, optionally with a mongoc_trans‐
41         action_opt_t that overrides the options  inherited  from  collection,
42         and  use mongoc_client_session_append to add the session to opts. See
43         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

DESCRIPTION

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

ERRORS

69       Errors are propagated via the error parameter.
70

RETURNS

72       -1 on failure, otherwise the number of documents counted.
73

EXAMPLE

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

MIGRATING FROM DEPRECATED COUNT FUNCTIONS

98       When migrating to mongoc_collection_count_documents from the deprecated
99       mongoc_collection_count  or mongoc_collection_count_with_opts, the fol‐
100       lowing query operators in the filter must be replaced:
101
102                     ┌────────────┬────────────────────────────┐
103                     │Operator    │ Replacement                │
104                     ├────────────┼────────────────────────────┤
105                     │$where      │ $expr
106                     ├────────────┼────────────────────────────┤
107                     │$near       │ $geoWithin with $center
108                     ├────────────┼────────────────────────────┤
109                     │$nearSphere │ $geoWithin            with │
110                     │            │ $centerSphere
111                     └────────────┴────────────────────────────┘
112
113       $expr requires MongoDB 3.6+
114

SEE ALSO

116       mongoc_collection_estimated_document_count()
117

AUTHOR

119       MongoDB, Inc
120
122       2017-present, MongoDB, Inc
123
124
125
126
1271.17.6                           Jun 03, 20M2O1NGOC_COLLECTION_COUNT_DOCUMENTS(3)
Impressum