MONGOC_COLLECTION_AGGREGATE(3)
π MONGOC_COLLECTION_AGGREGATE π
okay bestie, so this is a page from the libmongoc manual and it's all about one function: mongoc_collection_aggregate. it's giving "let me run an aggregation for you" and honestly? iconic. let's get into it.
β¨ SYNOPSIS β¨
mongoc_cursor_t *
mongoc_collection_aggregate (mongoc_collection_t *collection,
mongoc_query_flags_t flags,
const bson_t *pipeline,
const bson_t *opts,
const mongoc_read_prefs_t *read_prefs);
π PARAMETERS π
collectionβ amongoc_collection_t. your fave collection, obviously.flagsβ amongoc_query_flags_t. heads up: not all flag values apply here. in particular, settingMONGOC_QUERY_EXHAUSTresults in an error. so likeβ¦ don't do that. πpipelineβ abson_t, either a BSON array or a BSON document containing an array field named"pipeline".optsβ abson_tcontaining options for the command, orNULL.read_prefsβ amongoc_read_prefs_torNULL.
opts may be NULL or a BSON document with additional command options. here's the whole menu bestie:
readConcernβ construct amongoc_read_concern_tand usemongoc_read_concern_append()to add the read concern toopts. peep the example code formongoc_client_read_command_with_opts(). read concern requires MongoDB 3.2 or later, otherwise an error is returned.writeConcernβ construct amongoc_write_concern_tand usemongoc_write_concern_append()to add the write concern toopts. see the example code formongoc_client_write_command_with_opts().sessionIdβ first, construct amongoc_client_session_twithmongoc_client_start_session(). you can begin a transaction withmongoc_client_session_start_transaction(), optionally with amongoc_transaction_opt_tthat overrides the options inherited fromcollection, and usemongoc_client_session_append()to add the session toopts. see the example code formongoc_client_session_t.bypassDocumentValidationβ set totrueto skip server-side schema validation of the provided BSON documents. living dangerously, love that for you.collationβ configure textual comparisons. see Setting Collation Order, and the MongoDB Manual entry on Collation. collation requires MongoDB 3.2 or later, otherwise an error is returned.serverIdβ to target a specific server, include an int32"serverId"field. obtain the id by callingmongoc_client_select_server(), thenmongoc_server_description_id()on its return value.batchSizeβ an int32 representing number of documents requested to be returned on each call tomongoc_cursor_next()letβ a BSON document consisting of any number of parameter names, each followed by definitions of constants in the MQL Aggregate Expression language.commentβ abson_value_tspecifying the comment to attach to this command. the comment will appear in log messages, profiler output, and currentOp output. only string values are supported prior to MongoDB 4.4.hintβ a document or string that specifies the index to use to support the query predicate.
for a list of all options, see the MongoDB Manual entry on the aggregate command. π«Ά
big mood alert: this function is considered a retryable read operation unless the pipeline contains a write stage like $out or $merge. upon a transient error (a network error, errors due to replica set failover, etc.) the operation is safely retried once. if retryreads is false in the URI (see mongoc_uri_t) the retry behavior does not apply.
π¦ DESCRIPTION π¦
so what does it actually do? this function creates a cursor which sends the aggregate command on the underlying collection upon the first call to mongoc_cursor_next(). for more information on building aggregation pipelines, see the MongoDB Manual entry on the aggregate command.
read preferences, read and write concern, and collation can be overridden by various sources. the highest-priority sources for these options are listed first in the following table. (ok it was a table in the original but we're doing lists now, deal with it π )
Read Preferences, highest priority first:
read_prefs- Transaction
- collection
Read Concern, highest priority first:
opts- Transaction
- collection
Write Concern, highest priority first:
opts- Transaction
- collection
Collation, highest priority first:
opts
in a transaction, read concern and write concern are prohibited in opts and the read preference must be primary or NULL. write concern is applied from opts, or if opts has no write concern and the aggregation pipeline includes "$out", the write concern is applied from collection. the write concern is omitted for MongoDB before 3.4. no cap.
see the example for transactions and for the "distinct" command with opts. β¨
πΈ RETURNS πΈ
this function returns a newly allocated mongoc_cursor_t that should be freed with mongoc_cursor_destroy() when no longer in use. the returned mongoc_cursor_t is never NULL, even on error. the user must call mongoc_cursor_next() on the returned mongoc_cursor_t to execute the initial command.
cursor errors can be checked with mongoc_cursor_error_document(). it always fills out the bson_error_t if an error occurred, and optionally includes a server reply document if the error occurred server-side.
β οΈ WARNING: failure to handle the result of this function is a programming error. like, genuinely. don't be that girl. π
π§ EXAMPLE π§
#include <bson/bson.h>
#include <mongoc/mongoc.h>
static mongoc_cursor_t *
pipeline_query (mongoc_collection_t *collection)
{
mongoc_cursor_t *cursor;
bson_t *pipeline;
pipeline = BCON_NEW ("pipeline",
"[",
"{",
"$match",
"{",
"foo",
BCON_UTF8 ("A"),
"}",
"}",
"{",
"$match",
"{",
"bar",
BCON_BOOL (false),
"}",
"}",
"]");
cursor = mongoc_collection_aggregate (
collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
bson_destroy (pipeline);
return cursor;
}
π OTHER PARAMETERS π
when using $out, the pipeline stage that writes, the write_concern field of the mongoc_cursor_t will be set to the mongoc_write_concern_t parameter, if it is valid, and applied to the write command when mongoc_cursor_next() is called. pass any other parameters to the aggregate command, besides pipeline, as fields in opts:
mongoc_write_concern_t *write_concern = mongoc_write_concern_new ();
mongoc_write_concern_set_w (write_concern, 3);
pipeline =
BCON_NEW ("pipeline", "[", "{", "$out", BCON_UTF8 ("collection2"), "}", "]");
opts = BCON_NEW ("bypassDocumentValidation", BCON_BOOL (true));
mongoc_write_concern_append (write_concern, opts);
cursor = mongoc_collection_aggregate (
collection1, MONGOC_QUERY_NONE, pipeline, opts, NULL);
β AUTHOR β
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
and that's the whole page bestie!! π shoutout to version 2.5.6, dated Oct 09, 2026, and it's all from the libmongoc crew β MONGOC_COLLECTION_AGGREGATE(3), periodt. πβ¨
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms