1MONGOC_DATABASE_AGGREGATE(3) libmongoc MONGOC_DATABASE_AGGREGATE(3)
2
3
4
6 mongoc_database_aggregate - mongoc_database_aggregate()
7
9 mongoc_cursor_t *
10 mongoc_database_aggregate (mongoc_database_t *database,
11 const bson_t *pipeline,
12 const bson_t *opts,
13 const mongoc_read_prefs_t *read_prefs)
14 BSON_GNUC_WARN_UNUSED_RESULT;
15
17 • database: A mongoc_database_t.
18
19 • pipeline: A bson_t, either a BSON array or a BSON document containing
20 an array field named "pipeline".
21
22 • opts: A bson_t containing options for the command, or NULL.
23
24 • read_prefs: A mongoc_read_prefs_t or NULL.
25
26 opts may be NULL or a BSON document with additional command options:
27
28 • readConcern: Construct a mongoc_read_concern_t and use
29 mongoc_read_concern_append() to add the read concern to opts. See the
30 example code for mongoc_client_read_command_with_opts(). Read concern
31 requires MongoDB 3.2 or later, otherwise an error is returned.
32
33 • writeConcern: Construct a mongoc_write_concern_t and use
34 mongoc_write_concern_append() to add the write concern to opts. See
35 the example code for mongoc_client_write_command_with_opts().
36
37 • sessionId: First, construct a mongoc_client_session_t with
38 mongoc_client_start_session(). You can begin a transaction with
39 mongoc_client_session_start_transaction(), optionally with a
40 mongoc_transaction_opt_t that overrides the options inherited from
41 database, and use mongoc_client_session_append() to add the session
42 to opts. See the example code for mongoc_client_session_t.
43
44 • bypassDocumentValidation: Set to true to skip server-side schema val‐
45 idation of the provided BSON documents.
46
47 • collation: Configure textual comparisons. See Setting Collation Or‐
48 der, and the MongoDB Manual entry on Collation. Collation requires
49 MongoDB 3.2 or later, otherwise an error is returned.
50
51 • serverId: To target a specific server, include an int32 "serverId"
52 field. Obtain the id by calling mongoc_client_select_server(), then
53 mongoc_server_description_id() on its return value.
54
55 • batchSize: An int32 representing number of documents requested to be
56 returned on each call to mongoc_cursor_next()
57
58 • let: A BSON document consisting of any number of parameter names,
59 each followed by definitions of constants in the MQL Aggregate Ex‐
60 pression language.
61
62 • comment: A bson_value_t specifying the comment to attach to this com‐
63 mand. The comment will appear in log messages, profiler output, and
64 currentOp output. Only string values are supported prior to MongoDB
65 4.4.
66
67 • hint: A document or string that specifies the index to use to support
68 the query predicate.
69
70 For a list of all options, see the MongoDB Manual entry on the aggre‐
71 gate command.
72
74 This function creates a cursor which sends the aggregate command on the
75 underlying database upon the first call to mongoc_cursor_next(). For
76 more information on building aggregation pipelines, see the MongoDB
77 Manual entry on the aggregate command. Note that the pipeline must
78 start with a compatible stage that does not require an underlying col‐
79 lection (e.g. "$currentOp", "$listLocalSessions").
80
81 Read preferences, read and write concern, and collation can be overrid‐
82 den by various sources. The highest-priority sources for these options
83 are listed first in the following table. In a transaction, read concern
84 and write concern are prohibited in opts and the read preference must
85 be primary or NULL. Write concern is applied from opts, or if opts has
86 no write concern and the aggregation pipeline includes "$out", the
87 write concern is applied from database.
88
89 ┌─────────────────┬──────────────┬───────────────┬───────────┐
90 │Read Preferences │ Read Concern │ Write Concern │ Collation │
91 ├─────────────────┼──────────────┼───────────────┼───────────┤
92 │read_prefs │ opts │ opts │ opts │
93 ├─────────────────┼──────────────┼───────────────┼───────────┤
94 │Transaction │ Transaction │ Transaction │ │
95 ├─────────────────┼──────────────┼───────────────┼───────────┤
96 │database │ database │ database │ │
97 └─────────────────┴──────────────┴───────────────┴───────────┘
98
99 See the example for transactions and for the "distinct" command with
100 opts.
101
102 This function is considered a retryable read operation unless the pipe‐
103 line contains a write stage like $out or $merge. Upon a transient er‐
104 ror (a network error, errors due to replica set failover, etc.) the op‐
105 eration is safely retried once. If retryreads is false in the URI (see
106 mongoc_uri_t) the retry behavior does not apply.
107
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
124 #include <bson/bson.h>
125 #include <mongoc/mongoc.h>
126
127 static mongoc_cursor_t *
128 current_op_query (mongoc_client_t *client)
129 {
130 mongoc_cursor_t *cursor;
131 mongoc_database_t *database;
132 bson_t *pipeline;
133
134 pipeline = BCON_NEW ("pipeline",
135 "[",
136 "{",
137 "$currentOp",
138 "{",
139 "}",
140 "}",
141 "]");
142
143 /* $currentOp must be run on the admin database */
144 database = mongoc_client_get_database (client, "admin");
145
146 cursor = mongoc_database_aggregate (
147 database, pipeline, NULL, NULL);
148
149 bson_destroy (pipeline);
150 mongoc_database_destroy (database);
151
152 return cursor;
153 }
154
156 MongoDB, Inc
157
159 2017-present, MongoDB, Inc
160
161
162
163
1641.24.3 Aug 17, 2023 MONGOC_DATABASE_AGGREGATE(3)