1MONGOC_COLLECTION_AGGREGATE(3)     libmongoc    MONGOC_COLLECTION_AGGREGATE(3)
2
3
4

SYNOPSIS

6          mongoc_cursor_t *
7          mongoc_collection_aggregate (mongoc_collection_t *collection,
8                                       mongoc_query_flags_t flags,
9                                       const bson_t *pipeline,
10                                       const bson_t *opts,
11                                       const mongoc_read_prefs_t *read_prefs)
12             BSON_GNUC_WARN_UNUSED_RESULT;
13

PARAMETERS

15collection: A mongoc_collection_t.
16
17flags: A mongoc_query_flags_t. Not all flag values apply. In particu‐
18         lar, setting MONGOC_QUERY_EXHAUST results in an error.
19
20pipeline: A bson_t, either a BSON array or a BSON document containing
21         an array field named "pipeline".
22
23opts: A bson_t containing options for the command, or NULL.
24
25read_prefs: A mongoc_read_prefs_t or NULL.
26
27       opts may be NULL or a BSON document with additional command options:
28
29readConcern:    Construct    a    mongoc_read_concern_t    and    use
30         mongoc_read_concern_append() to add the read concern to opts. See the
31         example code for mongoc_client_read_command_with_opts(). Read concern
32         requires MongoDB 3.2 or later, otherwise an error is returned.
33
34writeConcern:   Construct   a    mongoc_write_concern_t    and    use
35         mongoc_write_concern_append()  to  add the write concern to opts. See
36         the example code for mongoc_client_write_command_with_opts().
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
45bypassDocumentValidation: Set to true to skip server-side schema val‐
46         idation of the provided BSON documents.
47
48collation: Configure textual comparisons. See Setting  Collation  Or‐
49         der,  and  the  MongoDB Manual entry on Collation. Collation requires
50         MongoDB 3.2 or later, otherwise an error is returned.
51
52serverId: To target a specific server, include  an  int32  "serverId"
53         field.  Obtain  the id by calling mongoc_client_select_server(), then
54         mongoc_server_description_id() on its return value.
55
56batchSize: An int32 representing number of documents requested to  be
57         returned on each call to mongoc_cursor_next()
58
59let:  A  BSON  document  consisting of any number of parameter names,
60         each followed by definitions of constants in the  MQL  Aggregate  Ex‐
61         pression language.
62
63comment: A bson_value_t specifying the comment to attach to this com‐
64         mand. The comment will appear in log messages, profiler  output,  and
65         currentOp  output.  Only string values are supported prior to MongoDB
66         4.4.
67
68hint: A document or string that specifies the index to use to support
69         the query predicate.
70
71       For  a  list of all options, see the MongoDB Manual entry on the aggre‐
72       gate command.
73
74       This function is considered a retryable read operation unless the pipe‐
75       line  contains a write stage like $out or $merge.  Upon a transient er‐
76       ror (a network error, errors due to replica set failover, etc.) the op‐
77       eration is safely retried once.  If retryreads is false in the URI (see
78       mongoc_uri_t) the retry behavior does not apply.
79

DESCRIPTION

81       This function creates a cursor which sends the aggregate command on the
82       underlying  collection upon the first call to mongoc_cursor_next(). For
83       more information on building aggregation  pipelines,  see  the  MongoDB
84       Manual entry on the aggregate command.
85
86       Read preferences, read and write concern, and collation can be overrid‐
87       den by various sources. The highest-priority sources for these  options
88       are listed first in the following table. In a transaction, read concern
89       and write concern are prohibited in opts and the read  preference  must
90       be  primary or NULL. Write concern is applied from opts, or if opts has
91       no write concern and the  aggregation  pipeline  includes  "$out",  the
92       write  concern is applied from collection. The write concern is omitted
93       for MongoDB before 3.4.
94
95            ┌─────────────────┬──────────────┬───────────────┬───────────┐
96            │Read Preferences │ Read Concern │ Write Concern │ Collation │
97            ├─────────────────┼──────────────┼───────────────┼───────────┤
98read_prefs       opts         opts          opts      
99            ├─────────────────┼──────────────┼───────────────┼───────────┤
100            │Transaction      │ Transaction  │ Transaction   │           │
101            ├─────────────────┼──────────────┼───────────────┼───────────┤
102collection       collection   collection    │           │
103            └─────────────────┴──────────────┴───────────────┴───────────┘
104
105       See the example for transactions and for the  "distinct"  command  with
106       opts.
107

RETURNS

109       This  function returns a newly allocated mongoc_cursor_t that should be
110       freed with mongoc_cursor_destroy() when no longer in use. The  returned
111       mongoc_cursor_t  is  never  NULL,  even  on  error.  The user must call
112       mongoc_cursor_next() on the returned  mongoc_cursor_t  to  execute  the
113       initial command.
114
115       Cursor  errors  can  be checked with mongoc_cursor_error_document(). It
116       always fills out the bson_error_t if an error occurred, and  optionally
117       includes a server reply document if the error occurred server-side.
118
119       WARNING:
120          Failure  to  handle the result of this function is a programming er‐
121          ror.
122

EXAMPLE

124          #include <bson/bson.h>
125          #include <mongoc/mongoc.h>
126
127          static mongoc_cursor_t *
128          pipeline_query (mongoc_collection_t *collection)
129          {
130             mongoc_cursor_t *cursor;
131             bson_t *pipeline;
132
133             pipeline = BCON_NEW ("pipeline",
134                                  "[",
135                                  "{",
136                                  "$match",
137                                  "{",
138                                  "foo",
139                                  BCON_UTF8 ("A"),
140                                  "}",
141                                  "}",
142                                  "{",
143                                  "$match",
144                                  "{",
145                                  "bar",
146                                  BCON_BOOL (false),
147                                  "}",
148                                  "}",
149                                  "]");
150
151             cursor = mongoc_collection_aggregate (
152                collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
153
154             bson_destroy (pipeline);
155
156             return cursor;
157          }
158

OTHER PARAMETERS

160       When using $out, the pipeline  stage  that  writes,  the  write_concern
161       field  of the mongoc_cursor_t will be set to the mongoc_write_concern_t
162       parameter, if it is valid,  and  applied  to  the  write  command  when
163       mongoc_cursor_next() is called. Pass any other parameters to the aggre‐
164       gate command, besides pipeline, as fields in opts:
165
166          mongoc_write_concern_t *write_concern = mongoc_write_concern_new ();
167          mongoc_write_concern_set_w (write_concern, 3);
168
169          pipeline =
170             BCON_NEW ("pipeline", "[", "{", "$out", BCON_UTF8 ("collection2"), "}", "]");
171
172          opts = BCON_NEW ("bypassDocumentValidation", BCON_BOOL (true));
173          mongoc_write_concern_append (write_concern, opts);
174
175          cursor = mongoc_collection_aggregate (
176             collection1, MONGOC_QUERY_NONE, pipeline, opts, NULL);
177

AUTHOR

179       MongoDB, Inc
180
182       2017-present, MongoDB, Inc
183
184
185
186
1871.25.1                           Nov 08, 2023   MONGOC_COLLECTION_AGGREGATE(3)
Impressum