123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- // Copyright 2014 MongoDB Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #pragma once
- #include <cstdint>
- #include <memory>
- #include <string>
- #include <bsoncxx/array/view.hpp>
- #include <bsoncxx/array/view_or_value.hpp>
- #include <bsoncxx/document/view.hpp>
- #include <bsoncxx/document/view_or_value.hpp>
- #include <mongocxx/config/prelude.hpp>
- namespace mongocxx {
- MONGOCXX_INLINE_NAMESPACE_BEGIN
- class client;
- class collection;
- class database;
- ///
- /// Class representing a MongoDB aggregation pipeline.
- ///
- class MONGOCXX_API pipeline {
- public:
- ///
- /// Creates a new aggregation pipeline.
- ///
- /// @see https://docs.mongodb.com/master/core/aggregation-pipeline/
- ///
- pipeline();
- ///
- /// Move constructs a pipeline.
- ///
- pipeline(pipeline&&) noexcept;
- ///
- /// Move assigns a pipeline.
- ///
- pipeline& operator=(pipeline&&) noexcept;
- ///
- /// Destroys a pipeline.
- ///
- ~pipeline();
- ///
- /// Adds new fields to documents.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/addFields/
- ///
- /// @param fields_to_add
- /// A document specifying the fields to add. For each field specified in this parameter, a
- /// corresponding field will be added to the documents, where the value of the added field is
- /// the result of evaluating the specified expression.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& add_fields(bsoncxx::document::view_or_value fields_to_add);
- ///
- /// Categorizes documents into groups, called buckets, based on a specified expression and
- /// bucket boundaries.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucket/
- ///
- /// @param bucket_args
- /// The specification for the bucket operation. The required fields `groupBy` and
- /// `boundaries` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& bucket(bsoncxx::document::view_or_value bucket_args);
- ///
- /// Categorizes documents into a specific number of groups, called buckets, based on a
- /// specified expression. Bucket boundaries are automatically determined in an attempt to
- /// evenly distribute the documents into the specified number of buckets.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucketAuto/
- ///
- /// @param bucket_auto_args
- /// The specification for the bucket_auto operation. This required fields `groupBy` and
- /// `buckets` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& bucket_auto(bsoncxx::document::view_or_value bucket_auto_args);
- ///
- /// Returns statistics regarding a collection or view.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/collStats/
- ///
- /// @param coll_stats_args
- /// The specification for the coll_stats operation. See link above for a list of valid
- /// options.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& coll_stats(
- bsoncxx::document::view_or_value coll_stats_args = bsoncxx::document::view{});
- ///
- /// Returns a document containing a count of the number of documents input to the stage.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/count/
- ///
- /// @param field
- /// Name of the field for the count to be written to.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& count(std::string field);
- ///
- /// Returns a stream of documents containing information on active and/or dormant
- /// operations as well as inactive sessions that are holding locks as part of a
- /// transaction. The stage returns a document for each operation or session.
- ///
- /// This stage must be used with database aggregate on the 'admin' database.
- ///
- /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/currentOp/
- ///
- /// @param current_op_args
- /// A document containing the arguments for the current_op operation.
- ///
- /// @return
- /// A reference to the object on which this method is being called.
- ///
- pipeline& current_op(bsoncxx::document::view_or_value current_op_args);
- ///
- /// Processes multiple aggregation pipelines within a single stage on the same set of input
- /// documents.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/facet/
- ///
- /// @param facet_args
- /// The specification for the facet operation. Each field in the the provided document should
- /// specify an aggregation pipeline, as an array.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& facet(bsoncxx::document::view_or_value facet_args);
- ///
- /// Appends a stage to this pipeline object.
- ///
- /// Use this method to run new pipeline stages that are not yet offered as
- /// explicit methods on this class. Pass in a document describing a
- /// pipeline stage:
- ///
- /// { "$addFields" : { "field" : 0 }}
- ///
- /// @param stage
- /// A bson document view or value describing a pipeline stage.
- ///
- /// @return
- /// A reference to this object on which this member function is being called.
- ///
- pipeline& append_stage(bsoncxx::document::view_or_value stage);
- ///
- /// Appends stages to this pipeline object from the given bson array.
- ///
- /// Use this method to run new pipeline stages that are not yet offered as
- /// explicit methods on this class. Pass in an array of documents, where each
- /// document in the array describes a pipeline stage:
- ///
- /// [ { "$addFields" : { "field" : 0 } }, { "$newStage" : ... }, ... ]
- ///
- /// @param stages
- /// A bson array containing documents describing pipeline stages.
- ///
- /// @return
- /// A reference to the object on which this member function is being called.
- ///
- pipeline& append_stages(bsoncxx::array::view_or_value stages);
- ///
- /// Outputs documents in order of nearest to farthest from a specified point.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/geoNear/
- ///
- /// @param geo_near_args
- /// The specification for the geo_near operation. The required fields `near` and
- /// `distanceField` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& geo_near(bsoncxx::document::view_or_value geo_near_args);
- ///
- /// Performs a recursive search on a collection.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/graphLookup/
- ///
- /// @param graph_lookup_args
- /// The specification for the graph_lookup operation. The required fields `from`,
- /// `connectFromField`, `startWith`, `connectToField`, and `as` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& graph_lookup(bsoncxx::document::view_or_value graph_lookup_args);
- ///
- /// Groups documents by some specified expression and outputs to the next stage a
- /// document for each distinct grouping. The output documents contain an `_id` field
- /// which contains the the distinct key for that group. The output documents can also
- /// contain computed fields that hold the values of some accumulator expression grouped
- /// by the group's `_id` field.
- ///
- /// @note group does not order output documents.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/group/
- ///
- /// @param group_args
- /// The specification for the group operation. The required field `_id` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& group(bsoncxx::document::view_or_value group_args);
- ///
- /// Returns statistics regarding the use of each index for the collection.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/indexStats/
- ///
- pipeline& index_stats();
- ///
- /// Limits the number of documents passed to the next stage in the pipeline.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/limit/
- ///
- /// @param limit
- /// The number of documents to which output should be limited.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& limit(std::int32_t limit);
- ///
- /// Lists the sessions cached in memory by the mongod or mongos instance.
- ///
- /// This option must be used with database aggregate.
- ///
- /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/listLocalSessions/
- ///
- /// @param list_local_sessions_args
- /// A document containing the arguments for list_local_sessions.
- ///
- /// @return
- /// A reference to the object on which this method is being called.
- ///
- pipeline& list_local_sessions(bsoncxx::document::view_or_value list_local_sessions_args);
- ///
- /// Lists all sessions stored in the system.sessions collection in the config database.
- /// These sessions are visible to all members of the MongoDB deployment.
- ///
- /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/listSessions/
- ///
- /// @param list_sessions_args
- /// A document containing the arguments for list_sessions.
- ///
- /// @return
- /// A reference to the object on which this method is being called.
- ///
- pipeline& list_sessions(bsoncxx::document::view_or_value list_sessions_args);
- ///
- /// Performs a left outer join to an unsharded collection in the same database to filter in
- /// documents from the "joined" collection for processing.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/lookup/
- ///
- /// @param lookup_args
- /// The specification for the lookup operation. The required fields `from`, `localField`,
- /// `foreignField`, and `as` must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& lookup(bsoncxx::document::view_or_value lookup_args);
- ///
- /// Filters the documents. Only the documents that match the condition(s) specified by the
- /// `filter` will continue to the next pipeline stage.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/match/
- ///
- /// @param filter
- /// The filter.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& match(bsoncxx::document::view_or_value filter);
- ///
- /// Outputs the aggregation results to a collection.
- ///
- /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/merge/
- ///
- /// @param merge_args
- /// The specification for the merge options. Must include an `into` field that
- /// is either a collection name or a subdocument of the form:
- /// { `db` : <db name>, `coll` : <collection name> }.
- ///
- /// @return
- /// A reference to the object on which this member function is being called.
- ///
- pipeline& merge(bsoncxx::document::view_or_value merge_args);
- ///
- /// Takes documents returned by the aggregation pipeline and writes them to a specified
- /// collection. This stage must be the last stage in the pipeline. The out operator lets the
- /// aggregation framework return result sets of any size.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/out/
- ///
- /// @param collection_name
- /// The name of the collection where the output documents should go.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& out(std::string collection_name);
- ///
- /// Projects a subset of the fields in the documents to the next stage of the pipeline.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/project/
- ///
- /// @param projection
- /// The projection specification.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& project(bsoncxx::document::view_or_value projection);
- ///
- /// Restricts the contents of the documents based on information stored in the documents
- /// themselves.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/redact/
- ///
- /// @param restrictions
- /// The document restrictions.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& redact(bsoncxx::document::view_or_value restrictions);
- ///
- /// Promotes a specified document to the top level and replaces all other fields.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/replaceRoot/
- ///
- /// @param replace_root_args
- /// The specification for the replace_root operation. The required field `newRoot` must be
- /// included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& replace_root(bsoncxx::document::view_or_value replace_root_args);
- ///
- /// Randomly selects the specified number of documents that pass into the stage and passes the
- /// remaining documents to the next stage in the pipeline.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sample/
- ///
- /// @param size
- /// The number of input documents to select.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& sample(std::int32_t size);
- ///
- /// Skips over the specified number of documents that pass into the stage and passes the
- /// remaining documents to the next stage in the pipeline.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/skip/
- ///
- /// @param docs_to_skip
- /// The number of input documents to skip.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& skip(std::int32_t docs_to_skip);
- ///
- /// Sorts all input documents and returns them to the pipeline in sorted order.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sort/
- ///
- /// @param ordering
- /// Document specifying the ordering by which the documents are sorted.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- pipeline& sort(bsoncxx::document::view_or_value ordering);
- ///
- /// Groups incoming documents based on the value of a specified expression, then computes the
- /// count of documents in each distinct group.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
- ///
- /// @param field_expression
- /// The expression to group by, as an object. The expression can not evaluate to an object.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- /// @note
- /// This overload of sort_by_count() is intended to be used when the desired sort is over a
- /// grouping of the result of a complex expression computed from the input documents.
- ///
- pipeline& sort_by_count(bsoncxx::document::view_or_value field_expression);
- ///
- /// Groups incoming documents based on the value of a specified expression, then computes the
- /// count of documents in each distinct group.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
- ///
- /// @param field_expression
- /// The expression to group by, as a string. To specify a field path, prefix the field path
- /// with a dollar sign (`$`).
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- /// @note
- /// This overload of sort_by_count() is intended to be used when the desired sort is over a
- /// grouping of the value of a particular element in the input documents.
- ///
- pipeline& sort_by_count(std::string field_expression);
- ///
- /// Deconstructs an array field from the input documents to output a document for each element.
- /// Each output document is an input document with the value of its array field replaced by
- /// an element from the unwound array.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/unwind/
- ///
- /// @param unwind_args
- /// The specification for the unwind operation. The required field path must be included.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- /// @note
- /// This overload of unwind() is intended to be used when additional options other than the
- /// field name need to be specified.
- ///
- pipeline& unwind(bsoncxx::document::view_or_value unwind_args);
- ///
- /// Deconstructs an array field from the input documents to output a document for each element.
- /// Each output document is an input document with the value of its array field replaced by
- /// an element from the unwound array.
- ///
- /// @see https://docs.mongodb.com/master/reference/operator/aggregation/unwind/
- ///
- /// @param field_name
- /// The name of the field to unwind.
- ///
- /// @return
- /// A reference to the object on which this member function is being called. This facilitates
- /// method chaining.
- ///
- /// @note
- /// This overload of unwind() is intended to be used when no options other than the field name
- /// need to be specified.
- ///
- pipeline& unwind(std::string field_name);
- ///
- /// @return A view of the underlying BSON array this pipeline represents.
- ///
- bsoncxx::array::view view_array() const;
- private:
- friend class client;
- friend class collection;
- friend class database;
- class MONGOCXX_PRIVATE impl;
- std::unique_ptr<impl> _impl;
- };
- MONGOCXX_INLINE_NAMESPACE_END
- } // namespace mongocxx
- #include <mongocxx/config/postlude.hpp>
|