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

18       · collection: A mongoc_collection_t.
19
20       · flags: A mongoc_query_flags_t.
21
22       · pipeline: A bson_t, either a BSON array or a BSON document containing
23         an array field named "pipeline".
24
25       · opts: A bson_t containing options for the command, or NULL.
26
27       · read_prefs: A mongoc_read_prefs_t or NULL.
28
29       opts may be NULL or a BSON document with additional command options:
30
31       · readConcern:  Construct  a   mongoc_read_concern_t   and   use   mon‐
32         goc_read_concern_append  to  add  the  read  concern to opts. See the
33         example code for mongoc_client_read_command_with_opts.  Read  concern
34         requires MongoDB 3.2 or later, otherwise an error is returned.
35
36       · writeConcern:   Construct   a  mongoc_write_concern_t  and  use  mon‐
37         goc_write_concern_append to add the write concern to  opts.  See  the
38         example code for mongoc_client_write_command_with_opts.
39
40       · sessionId:  First,  construct  a  mongoc_client_session_t  with  mon‐
41         goc_client_start_session. You  can  begin  a  transaction  with  mon‐
42         goc_client_session_start_transaction, optionally with a mongoc_trans‐
43         action_opt_t that overrides the options  inherited  from  collection,
44         and  use mongoc_client_session_append to add the session to opts. See
45         the example code for mongoc_client_session_t.
46
47       · bypassDocumentValidation: Set to true to skip server-side schema val‐
48         idation of the provided BSON documents.
49
50       · collation:  Configure  textual  comparisons.  See  Setting  Collation
51         Order, and the MongoDB Manual entry on Collation. Collation  requires
52         MongoDB 3.2 or later, otherwise an error is returned.
53
54       · serverId:  To  target  a specific server, include an int32 "serverId"
55         field. Obtain the id  by  calling  mongoc_client_select_server,  then
56         mongoc_server_description_id on its return value.
57
58       · batchSize:  An int32 representing number of documents requested to be
59         returned on each call to mongoc_cursor_next
60
61       For a list of all options, see the MongoDB Manual entry on  the  aggre‐
62       gate command.
63
64       This function is considered a retryable read operation unless the pipe‐
65       line contains a write stage like $out  or  $merge.   Upon  a  transient
66       error  (a  network error, errors due to replica set failover, etc.) the
67       operation is safely retried once.  If retryreads is false  in  the  URI
68       (see mongoc_uri_t) the retry behavior does not apply.
69

DESCRIPTION

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

RETURNS

99       This function returns a newly allocated mongoc_cursor_t that should  be
100       freed  with mongoc_cursor_destroy() when no longer in use. The returned
101       mongoc_cursor_t is never NULL;  if  the  parameters  are  invalid,  the
102       bson_error_t  in the mongoc_cursor_t is filled out, and the mongoc_cur‐
103       sor_t is returned before the server is selected.  The  user  must  call
104       mongoc_cursor_next()  on  the  returned  mongoc_cursor_t to execute the
105       aggregation pipeline.
106
107       WARNING:
108          Failure to handle the result  of  this  function  is  a  programming
109          error.
110

EXAMPLE

112          #include <bson/bson.h>
113          #include <mongoc/mongoc.h>
114
115          static mongoc_cursor_t *
116          pipeline_query (mongoc_collection_t *collection)
117          {
118             mongoc_cursor_t *cursor;
119             bson_t *pipeline;
120
121             pipeline = BCON_NEW ("pipeline",
122                                  "[",
123                                  "{",
124                                  "$match",
125                                  "{",
126                                  "foo",
127                                  BCON_UTF8 ("A"),
128                                  "}",
129                                  "}",
130                                  "{",
131                                  "$match",
132                                  "{",
133                                  "bar",
134                                  BCON_BOOL (false),
135                                  "}",
136                                  "}",
137                                  "]");
138
139             cursor = mongoc_collection_aggregate (
140                collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
141
142             bson_destroy (pipeline);
143
144             return cursor;
145          }
146

OTHER PARAMETERS

148       When  using  $out,  the  pipeline  stage that writes, the write_concern
149       field of the mongoc_cursor_t will be set to the  mongoc_write_concern_t
150       parameter,  if  it is valid, and applied to the write command when mon‐
151       goc_cursor_next() is called. Pass any other parameters to the aggregate
152       command, besides pipeline, as fields in opts:
153
154          mongoc_write_concern_t *write_concern = mongoc_write_concern_new ();
155          mongoc_write_concern_set_w (write_concern, 3);
156
157          pipeline =
158             BCON_NEW ("pipeline", "[", "{", "$out", BCON_UTF8 ("collection2"), "}", "]");
159
160          opts = BCON_NEW ("bypassDocumentValidation", BCON_BOOL (true));
161          mongoc_write_concern_append (write_concern, opts);
162
163          cursor = mongoc_collection_aggregate (
164             collection1, MONGOC_QUERY_NONE, pipeline, opts, NULL);
165

AUTHOR

167       MongoDB, Inc
168
170       2017-present, MongoDB, Inc
171
172
173
174
1751.16.2                           Feb 25, 2020   MONGOC_COLLECTION_AGGREGATE(3)
Impressum