MONGOC_DATABASE_AGGREGATE(3)
πΈ MONGOC_DATABASE_AGGREGATE πΈ
ok so bestie, this is a page from libmongoc β the mongoc_database_aggregate(3) manual, and no cap it's actually kind of a big deal. it's giving "database-wide aggregation era" π
so let's get into it.
β¨ SYNOPSIS β¨
like, here's the whole vibe of the function signature, exactly as it is:
mongoc_cursor_t *
mongoc_database_aggregate (mongoc_database_t *database,
const bson_t *pipeline,
const bson_t *opts,
const mongoc_read_prefs_t *read_prefs);
π PARAMETERS π
okay so here's who's who in this little group chat π
databaseβ amongoc_database_t. that's the database you're aggregating on, periodt.pipelineβ abson_t, and it's either a BSON array or a BSON document containing an array field named"pipeline". both are valid, pick your fighter β¨optsβ abson_tholding options for the command, orNULL. no pressure.read_prefsβ amongoc_read_prefs_t, orNULL. also no pressure.
ngl, opts can be NULL or a BSON document with extra command options, and these are them π
readConcernβ build amongoc_read_concern_tand usemongoc_read_concern_append()to add the read concern toopts. see the example code formongoc_client_read_command_with_opts(). btw read concern needs MongoDB 3.2 or later β otherwise an error is returned. πwriteConcernβ build 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 fromdatabase, and usemongoc_client_session_append()to add the session toopts. see the example code formongoc_client_session_t. iykyk π«Ά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. same energy as read concern fr fr.serverIdβ to target a specific server, include an int32"serverId"field. get the id by callingmongoc_client_select_server(), thenmongoc_server_description_id()on its return value. very main character of you to want one specific server π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. fancy variable stuff, we love to see it β¨commentβ abson_value_tspecifying the comment to attach to this command. the comment will appear in log messages, profiler output, andcurrentOpoutput. 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.
and for the full list of literally every option, see the MongoDB Manual entry on the aggregate command. i'm not gatekeeping, i'm just quoting the source π¦
π DESCRIPTION π
okay so what does this actually do. this function creates a cursor which sends the aggregate command on the underlying database upon the first call to mongoc_cursor_next(). yes, the first one. it's a "we'll get there when we get there" situation π
for more info on building aggregation pipelines, see the MongoDB Manual entry on the aggregate command. also β huge caveat β the pipeline must start with a compatible stage that does not require an underlying collection (e.g. "$currentOp", "$listLocalSessions"). like, this is a database-level aggregate, so, yeah. π₯
read preferences, read and write concern, and collation can be overridden by various sources. the highest-priority sources for these options are listed first. so in priority order, from most to least:
- top of the food chain β read preferences come from
read_prefs; read concern fromopts; write concern fromopts; collation fromopts. π - next down β read preferences come from the Transaction; read concern from the Transaction; write concern from the Transaction. (collation has nothing at this level, so, crickets.)
- last resort β read preferences come from
database; read concern fromdatabase; write concern fromdatabase. (again, no collation at this level.)
in a transaction, read concern and write concern are prohibited in opts, and the read preference must be primary or NULL. that's not a suggestion, bestie, that's the rule π
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 database.
see the example for transactions and for the "distinct" command with opts.
ngl this part is important: 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. okay so that's your freebie, use it wisely β¨
π¦ 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 β iconic, honestly. but you (yes, you) must call mongoc_cursor_next() on the returned mongoc_cursor_t to execute the initial command. don't just sit there holding it π«Ά
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, fr fr. check your errors. π
π EXAMPLE π
here's the whole thing, copied exactly, it's giving $currentOp realness:
#include <bson/bson.h>
#include <mongoc/mongoc.h>
static mongoc_cursor_t *
current_op_query (mongoc_client_t *client)
{
mongoc_cursor_t *cursor;
mongoc_database_t *database;
bson_t *pipeline;
pipeline = BCON_NEW ("pipeline",
"[",
"{",
"$currentOp",
"{",
"}",
"}",
"]");
/* $currentOp must be run on the admin database */
database = mongoc_client_get_database (client, "admin");
cursor = mongoc_database_aggregate (
database, pipeline, NULL, NULL);
bson_destroy (pipeline);
mongoc_database_destroy (database);
return cursor;
}
π AUTHOR π
MongoDB, Inc β the icons themselves.
β COPYRIGHT β
2009-present, MongoDB, Inc. β that's the whole legal vibe, slay.
and that's the page, bestie! π this was libmongoc 2.5.6, dated Oct 09, 2026, and the header up top said MONGOC_DATABASE_AGGREGATE(3) the whole time. go forth and aggregate responsibly πβ¨π¦
π¦ 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