1MONGOC_DATABASE_AGGREGATE(3) MongoDB C Driver 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 mon‐
29 goc_read_concern_append to add the read concern to opts. See the
30 example code for mongoc_client_read_command_with_opts.
31
32 · writeConcern: For aggregations that include "$out", you can construct
33 a mongoc_write_concern_t and use mongoc_write_concern_append to add
34 the write concern to opts. See the example code for mon‐
35 goc_client_write_command_with_opts.
36
37 · sessionId: Construct a mongoc_client_session_t with mon‐
38 goc_client_start_session and use mongoc_client_session_append to add
39 the session to opts. See the example code for mongoc_client_ses‐
40 sion_t.
41
42 · bypassDocumentValidation: Set to true to skip server-side schema val‐
43 idation of the provided BSON documents.
44
45 · collation: Configure textual comparisons. See Setting Collation
46 Order, and the MongoDB Manual entry on Collation.
47
48 · serverId: To target a specific server, include an int32 "serverId"
49 field. Obtain the id by calling mongoc_client_select_server, then
50 mongoc_server_description_id on its return value.
51
52 · batchSize: To specify the number of documents to return in each batch
53 of a response from the server, include an int "batchSize" field.
54
55 For a list of all options, see the MongoDB Manual entry on the aggre‐
56 gate command.
57
59 This function creates a cursor which sends the aggregate command on the
60 underlying database upon the first call to mongoc_cursor_next(). For
61 more information on building aggregation pipelines, see the MongoDB
62 Manual entry on the aggregate command. Note that the pipeline must
63 start with a compatible stage that does not require an underlying col‐
64 lection (e.g. "$currentOp", "$listLocalSessions").
65
66 Read preferences, read and write concern, and collation can be overrid‐
67 den by various sources. The highest-priority sources for these options
68 are listed first in the following table. In a transaction, read concern
69 and write concern are prohibited in opts and the read preference must
70 be primary or NULL. Write concern is applied from opts, or if opts has
71 no write concern and the aggregation pipeline includes "$out", the
72 write concern is applied from database.
73
74 ┌─────────────────┬──────────────┬───────────────┬───────────┐
75 │Read Preferences │ Read Concern │ Write Concern │ Collation │
76 ├─────────────────┼──────────────┼───────────────┼───────────┤
77 │read_prefs │ opts │ opts │ opts │
78 ├─────────────────┼──────────────┼───────────────┼───────────┤
79 │Transaction │ Transaction │ Transaction │ │
80 ├─────────────────┼──────────────┼───────────────┼───────────┤
81 │database │ database │ database │ │
82 └─────────────────┴──────────────┴───────────────┴───────────┘
83
84 See the example for transactions and for the "distinct" command with
85 opts.
86
87 This function is considered a retryable read operation unless the pipe‐
88 line contains a write stage like $out or $merge. Upon a transient
89 error (a network error, errors due to replica set failover, etc.) the
90 operation is safely retried once. If retryreads is false in the URI
91 (see mongoc_uri_t) the retry behavior does not apply.
92
94 This function returns a newly allocated mongoc_cursor_t that should be
95 freed with mongoc_cursor_destroy() when no longer in use. The returned
96 mongoc_cursor_t is never NULL; if the parameters are invalid, the
97 bson_error_t in the mongoc_cursor_t is filled out, and the mongoc_cur‐
98 sor_t is returned before the server is selected. The user must call
99 mongoc_cursor_next() on the returned mongoc_cursor_t to execute the
100 aggregation pipeline.
101
102 WARNING:
103 Failure to handle the result of this function is a programming
104 error.
105
107 #include <bson/bson.h>
108 #include <mongoc/mongoc.h>
109
110 static mongoc_cursor_t *
111 current_op_query (mongoc_client_t *client)
112 {
113 mongoc_cursor_t *cursor;
114 mongoc_database_t *database;
115 bson_t *pipeline;
116
117 pipeline = BCON_NEW ("pipeline",
118 "[",
119 "{",
120 "$currentOp",
121 "{",
122 "}",
123 "}",
124 "]");
125
126 /* $currentOp must be run on the admin database */
127 database = mongoc_client_get_database (client, "admin");
128
129 cursor = mongoc_database_aggregate (
130 database, pipeline, NULL, NULL);
131
132 bson_destroy (pipeline);
133 mongoc_database_destroy (database);
134
135 return cursor;
136 }
137
139 MongoDB, Inc
140
142 2017-present, MongoDB, Inc
143
144
145
146
1471.15.2 Nov 06, 2019 MONGOC_DATABASE_AGGREGATE(3)