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

NAME

6       mongoc_collection_aggregate - mongoc_collection_aggregate()
7

SYNOPSIS

9          mongoc_cursor_t *
10          mongoc_collection_aggregate (mongoc_collection_t *collection,
11                                       mongoc_query_flags_t flags,
12                                       const bson_t *pipeline,
13                                       const bson_t *opts,
14                                       const mongoc_read_prefs_t *read_prefs)
15             BSON_GNUC_WARN_UNUSED_RESULT;
16

PARAMETERS

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

DESCRIPTION

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

RETURNS

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

EXAMPLE

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

OTHER PARAMETERS

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

AUTHOR

182       MongoDB, Inc
183
185       2017-present, MongoDB, Inc
186
187
188
189
1901.24.3                           Aug 17, 2023   MONGOC_COLLECTION_AGGREGATE(3)
Impressum