Skip to content

Commit 764886c

Browse files
committed
CXX-1059 Add new stage methods to pipeline class
As a result of this work, this pipeline class is now up-to-date with new functionality available in the aggregation framework as of server version 3.4. Complete list of methods added: pipeline& add_fields(bsoncxx::document::view_or_value fields_to_add); pipeline& bucket(bsoncxx::document::view_or_value bucket_args); pipeline& bucket_auto(bsoncxx::document::view_or_value bucket_auto_args); pipeline& coll_stats(bsoncxx::document::view_or_value coll_stats_args); pipeline& count(std::string field); pipeline& facet(bsoncxx::document::view_or_value facet_args); pipeline& geo_near(bsoncxx::document::view_or_value geo_near_args); pipeline& graph_lookup(bsoncxx::document::view_or_value graph_lookup_args); pipeline& index_stats(); pipeline& replace_root(bsoncxx::document::view_or_value replace_root_args); pipeline& sort_by_count(bsoncxx::document::view_or_value field_expression); pipeline& sort_by_count(std::string field_expression); pipeline& unwind(bsoncxx::document::view_or_value unwind_args);
1 parent 220ecf8 commit 764886c

File tree

3 files changed

+506
-4
lines changed

3 files changed

+506
-4
lines changed

src/mongocxx/pipeline.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,56 @@ pipeline::pipeline(pipeline&&) noexcept = default;
3232
pipeline& pipeline::operator=(pipeline&&) noexcept = default;
3333
pipeline::~pipeline() = default;
3434

35+
pipeline& pipeline::add_fields(bsoncxx::document::view_or_value fields_to_add) {
36+
_impl->sink() << open_document << "$addFields" << fields_to_add << close_document;
37+
return *this;
38+
}
39+
40+
pipeline& pipeline::bucket(bsoncxx::document::view_or_value bucket_args) {
41+
_impl->sink() << open_document << "$bucket" << bucket_args << close_document;
42+
return *this;
43+
}
44+
45+
pipeline& pipeline::bucket_auto(bsoncxx::document::view_or_value bucket_auto_args) {
46+
_impl->sink() << open_document << "$bucketAuto" << bucket_auto_args << close_document;
47+
return *this;
48+
}
49+
50+
pipeline& pipeline::coll_stats(bsoncxx::document::view_or_value coll_stats_args) {
51+
_impl->sink() << open_document << "$collStats" << coll_stats_args << close_document;
52+
return *this;
53+
}
54+
55+
pipeline& pipeline::count(std::string field) {
56+
_impl->sink() << open_document << "$count" << field << close_document;
57+
return *this;
58+
}
59+
60+
pipeline& pipeline::facet(bsoncxx::document::view_or_value facet_args) {
61+
_impl->sink() << open_document << "$facet" << facet_args << close_document;
62+
return *this;
63+
}
64+
65+
pipeline& pipeline::geo_near(bsoncxx::document::view_or_value geo_near_args) {
66+
_impl->sink() << open_document << "$geoNear" << geo_near_args << close_document;
67+
return *this;
68+
}
69+
70+
pipeline& pipeline::graph_lookup(bsoncxx::document::view_or_value graph_lookup_args) {
71+
_impl->sink() << open_document << "$graphLookup" << graph_lookup_args << close_document;
72+
return *this;
73+
}
74+
3575
pipeline& pipeline::group(bsoncxx::document::view_or_value group_args) {
3676
_impl->sink() << open_document << "$group" << group_args << close_document;
3777
return *this;
3878
}
3979

80+
pipeline& pipeline::index_stats() {
81+
_impl->sink() << open_document << "$indexStats" << bsoncxx::document::view{} << close_document;
82+
return *this;
83+
}
84+
4085
pipeline& pipeline::limit(std::int32_t limit) {
4186
_impl->sink() << open_document << "$limit" << limit << close_document;
4287
return *this;
@@ -67,6 +112,11 @@ pipeline& pipeline::redact(bsoncxx::document::view_or_value restrictions) {
67112
return *this;
68113
}
69114

115+
pipeline& pipeline::replace_root(bsoncxx::document::view_or_value replace_root_args) {
116+
_impl->sink() << open_document << "$replaceRoot" << replace_root_args << close_document;
117+
return *this;
118+
}
119+
70120
pipeline& pipeline::sample(std::int32_t size) {
71121
_impl->sink() << open_document << "$sample" << open_document << "size" << size << close_document
72122
<< close_document;
@@ -83,6 +133,21 @@ pipeline& pipeline::sort(bsoncxx::document::view_or_value ordering) {
83133
return *this;
84134
}
85135

136+
pipeline& pipeline::sort_by_count(bsoncxx::document::view_or_value field_expression) {
137+
_impl->sink() << open_document << "$sortByCount" << field_expression << close_document;
138+
return *this;
139+
}
140+
141+
pipeline& pipeline::sort_by_count(std::string field_expression) {
142+
_impl->sink() << open_document << "$sortByCount" << field_expression << close_document;
143+
return *this;
144+
}
145+
146+
pipeline& pipeline::unwind(bsoncxx::document::view_or_value unwind_args) {
147+
_impl->sink() << open_document << "$unwind" << unwind_args << close_document;
148+
return *this;
149+
}
150+
86151
pipeline& pipeline::unwind(std::string field_name) {
87152
_impl->sink() << open_document << "$unwind" << field_name << close_document;
88153
return *this;

src/mongocxx/pipeline.hpp

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,99 @@ class MONGOCXX_API pipeline {
5656
///
5757
~pipeline();
5858

59+
///
60+
/// Adds new fields to documents.
61+
///
62+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/addFields/
63+
///
64+
/// @param fields_to_add
65+
/// A document specifying the fields to add. For each field specified in this parameter, a
66+
/// corresponding field will be added to the documents, where the value of the added field is
67+
/// the result of evaluating the specified expression.
68+
///
69+
pipeline& add_fields(bsoncxx::document::view_or_value fields_to_add);
70+
71+
///
72+
/// Categorizes documents into groups, called buckets, based on a specified expression and
73+
/// bucket boundaries.
74+
///
75+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucket/
76+
///
77+
/// @param bucket_args
78+
/// The specification for the bucket operation. The required fields `groupBy` and
79+
/// `boundaries` must be included.
80+
///
81+
pipeline& bucket(bsoncxx::document::view_or_value bucket_args);
82+
83+
///
84+
/// Categorizes documents into a specific number of groups, called buckets, based on a
85+
/// specified expression. Bucket boundaries are automatically determined in an attempt to
86+
/// evenly distribute the documents into the specified number of buckets.
87+
///
88+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucketAuto/
89+
///
90+
/// @param bucket_auto_args
91+
/// The specification for the bucket_auto operation. This required fields `groupBy` and
92+
/// `buckets` must be included.
93+
///
94+
pipeline& bucket_auto(bsoncxx::document::view_or_value bucket_auto_args);
95+
96+
///
97+
/// Returns statistics regarding a collection or view.
98+
///
99+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/collStats/
100+
///
101+
/// @param coll_stats_args
102+
/// The specification for the coll_stats operation. See link above for a list of valid
103+
/// options.
104+
///
105+
pipeline& coll_stats(
106+
bsoncxx::document::view_or_value coll_stats_args = bsoncxx::document::view{});
107+
108+
///
109+
/// Returns a document containing a count of the number of documents input to the stage.
110+
///
111+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/count/
112+
///
113+
/// @param field
114+
/// Name of the field for the count to be written to.
115+
///
116+
pipeline& count(std::string field);
117+
118+
///
119+
/// Processes multiple aggregation pipelines within a single stage on the same set of input
120+
/// documents.
121+
///
122+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/facet/
123+
///
124+
/// @param facet_args
125+
/// The specification for the facet operation. Each field in the the provided document should
126+
/// specify an aggregation pipeline, as an array.
127+
///
128+
pipeline& facet(bsoncxx::document::view_or_value facet_args);
129+
130+
///
131+
/// Outputs documents in order of nearest to farthest from a specified point.
132+
///
133+
/// @see https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
134+
///
135+
/// @param geo_near_args
136+
/// The specification for the geo_near operation. The required fields `near` and
137+
/// `distanceField` must be included.
138+
///
139+
pipeline& geo_near(bsoncxx::document::view_or_value geo_near_args);
140+
141+
///
142+
/// Performs a recursive search on a collection.
143+
///
144+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/graphLookup/
145+
///
146+
/// @param graph_lookup_args
147+
/// The specification for the graph_lookup operation. The required fields `from`,
148+
/// `connectFromField`, `startWith`, `connectToField`, and `as` must be included.
149+
///
150+
pipeline& graph_lookup(bsoncxx::document::view_or_value graph_lookup_args);
151+
59152
///
60153
/// Groups documents by some specified expression and outputs to the next stage a
61154
/// document for each distinct grouping. The output documents contain an `_id` field
@@ -72,6 +165,13 @@ class MONGOCXX_API pipeline {
72165
///
73166
pipeline& group(bsoncxx::document::view_or_value group_args);
74167

168+
///
169+
/// Returns statistics regarding the use of each index for the collection.
170+
///
171+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/indexStats/
172+
///
173+
pipeline& index_stats();
174+
75175
///
76176
/// Limits the number of documents passed to the next stage in the pipeline.
77177
///
@@ -138,6 +238,17 @@ class MONGOCXX_API pipeline {
138238
///
139239
pipeline& redact(bsoncxx::document::view_or_value restrictions);
140240

241+
///
242+
/// Promotes a specified document to the top level and replaces all other fields.
243+
///
244+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/replaceRoot/
245+
///
246+
/// @param replace_root_args
247+
/// The specification for the replace_root operation. The required field `newRoot` must be
248+
/// included.
249+
///
250+
pipeline& replace_root(bsoncxx::document::view_or_value replace_root_args);
251+
141252
///
142253
/// Randomly selects the specified number of documents that pass into the stage and passes the
143254
/// remaining documents to the next stage in the pipeline.
@@ -170,6 +281,53 @@ class MONGOCXX_API pipeline {
170281
///
171282
pipeline& sort(bsoncxx::document::view_or_value ordering);
172283

284+
///
285+
/// Groups incoming documents based on the value of a specified expression, then computes the
286+
/// count of documents in each distinct group.
287+
///
288+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
289+
///
290+
/// @param field_expression
291+
/// The expression to group by, as an object. The expression can not evaluate to an object.
292+
///
293+
/// @note
294+
/// This overload of sort_by_count() is intended to be used when the desired sort is over a
295+
/// grouping of the result of a complex expression computed from the input documents.
296+
///
297+
pipeline& sort_by_count(bsoncxx::document::view_or_value field_expression);
298+
299+
///
300+
/// Groups incoming documents based on the value of a specified expression, then computes the
301+
/// count of documents in each distinct group.
302+
///
303+
/// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
304+
///
305+
/// @param field_expression
306+
/// The expression to group by, as a string. To specify a field path, prefix the field path
307+
/// with a dollar sign (`$`).
308+
///
309+
/// @note
310+
/// This overload of sort_by_count() is intended to be used when the desired sort is over a
311+
/// grouping of the value of a particular element in the input documents.
312+
///
313+
pipeline& sort_by_count(std::string field_expression);
314+
315+
///
316+
/// Deconstructs an array field from the input documents to output a document for each element.
317+
/// Each output document is an input document with the value of its array field replaced by
318+
/// an element from the unwound array.
319+
///
320+
/// @see http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/
321+
///
322+
/// @param unwind_args
323+
/// The specification for the unwind operation. The required field @path must be included.
324+
///
325+
/// @note
326+
/// This overload of unwind() is intended to be used when additional options other than the
327+
/// field name need to be specified.
328+
///
329+
pipeline& unwind(bsoncxx::document::view_or_value unwind_args);
330+
173331
///
174332
/// Deconstructs an array field from the input documents to output a document for each element.
175333
/// Each output document is an input document with the value of its array field replaced by
@@ -180,6 +338,10 @@ class MONGOCXX_API pipeline {
180338
/// @param field_name
181339
/// The name of the field to unwind.
182340
///
341+
/// @note
342+
/// This overload of unwind() is intended to be used when no options other than the field name
343+
/// need to be specified.
344+
///
183345
pipeline& unwind(std::string field_name);
184346

185347
///

0 commit comments

Comments
 (0)