File: mongoc_collection_count_with_opts.rst

package info (click to toggle)
mongo-c-driver 1.17.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 37,256 kB
  • sloc: ansic: 152,613; javascript: 7,954; python: 3,259; sh: 138; makefile: 29; xml: 10
file content (142 lines) | stat: -rw-r--r-- 4,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
:man_page: mongoc_collection_count_with_opts

mongoc_collection_count_with_opts()
===================================

Deprecated
----------

This function is deprecated and should not be used in new code.
Use :symbol:`mongoc_collection_count_documents` or :symbol:`mongoc_collection_estimated_document_count` instead.

:symbol:`mongoc_collection_count_documents` has similar performance to calling :symbol:`mongoc_collection_count` with a non-NULL ``query``, and is guaranteed to retrieve an accurate collection count. See :ref:`migrating from deprecated count functions <migrating-from-deprecated-count>` for details.

:symbol:`mongoc_collection_estimated_document_count` has the same performance as calling :symbol:`mongoc_collection_count` with a NULL ``query``, but is not guaranteed to retrieve an accurate collection count.

Synopsis
--------

.. code-block:: c

  int64_t
  mongoc_collection_count_with_opts (mongoc_collection_t *collection,
                                     mongoc_query_flags_t flags,
                                     const bson_t *query,
                                     int64_t skip,
                                     int64_t limit,
                                     const bson_t *opts,
                                     const mongoc_read_prefs_t *read_prefs,
                                     bson_error_t *error)
   BSON_GNUC_DEPRECATED_FOR (mongoc_collection_count_documents or
                             mongoc_collection_estimated_document_count);

Parameters
----------

* ``collection``: A :symbol:`mongoc_collection_t`.
* ``flags``: A :symbol:`mongoc_query_flags_t`.
* ``query``: A :symbol:`bson:bson_t` containing the query.
* ``skip``: A int64_t, zero to ignore.
* ``limit``: A int64_t, zero to ignore.
* ``opts``: A :symbol:`bson:bson_t`, ``NULL`` to ignore.
* ``read_prefs``: An optional :symbol:`mongoc_read_prefs_t`, otherwise uses the collection's read preference.
* ``error``: An optional location for a :symbol:`bson_error_t <errors>` or ``NULL``.

.. |opts-source| replace:: ``collection``

.. include:: includes/read-opts.txt

Description
-----------

This function shall execute a count query on the underlying 'collection'. The bson 'query' is not validated, simply passed along as appropriate to the server.  As such, compatibility and errors should be validated in the appropriate server documentation.

The :symbol:`mongoc_read_concern_t` specified on the :symbol:`mongoc_collection_t` will be used, if any. If ``read_prefs`` is NULL, the collection's read preferences are used.

In addition to the standard functionality available from mongoc_collection_count, this function allows the user to add arbitrary extra keys to the count.  This pass through enables features such as hinting for counts.

For more information, see the `query reference <http://docs.mongodb.org/manual/reference/operator/query/>`_ at the MongoDB website.

.. include:: includes/retryable-read.txt

Errors
------

Errors are propagated via the ``error`` parameter.

Returns
-------

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

Examples
--------

.. code-block:: c
  :caption: Basic Counting

  #include <bson/bson.h>
  #include <mongoc/mongoc.h>
  #include <stdio.h>

  static void
  print_query_count (mongoc_collection_t *collection, bson_t *query)
  {
     bson_error_t error;
     int64_t count;
     bson_t opts;

     bson_init (&opts);
     BSON_APPEND_UTF8 (&opts, "hint", "_id_");

     count = mongoc_collection_count_with_opts (
        collection, MONGOC_QUERY_NONE, query, 0, 0, &opts, NULL, &error);

     bson_destroy (&opts);

     if (count < 0) {
        fprintf (stderr, "Count failed: %s\n", error.message);
     } else {
        printf ("%" PRId64 " documents counted.\n", count);
     }
  }

.. code-block:: c
  :caption: Counting with Collation

  #include <bson/bson.h>
  #include <mongoc/mongoc.h>
  #include <stdio.h>

  static void
  print_query_count (mongoc_collection_t *collection, bson_t *query)
  {
     bson_t *selector;
     bson_t *opts;
     bson_error_t error;
     int64_t count;

     selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");

     /* "One" normally sorts before "one"; make "one" come first */
     opts = BCON_NEW ("collation",
                      "{",
                      "locale",
                      BCON_UTF8 ("en_US"),
                      "caseFirst",
                      BCON_UTF8 ("lower"),
                      "}");

     count = mongoc_collection_count_with_opts (
        collection, MONGOC_QUERY_NONE, query, 0, 0, opts, NULL, &error);

     bson_destroy (selector);
     bson_destroy (opts);

     if (count < 0) {
        fprintf (stderr, "Count failed: %s\n", error.message);
     } else {
        printf ("%" PRId64 " documents counted.\n", count);
     }
  }