pipeline.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <cstdint>
  16. #include <memory>
  17. #include <string>
  18. #include <bsoncxx/array/view.hpp>
  19. #include <bsoncxx/array/view_or_value.hpp>
  20. #include <bsoncxx/document/view.hpp>
  21. #include <bsoncxx/document/view_or_value.hpp>
  22. #include <mongocxx/config/prelude.hpp>
  23. namespace mongocxx {
  24. MONGOCXX_INLINE_NAMESPACE_BEGIN
  25. class client;
  26. class collection;
  27. class database;
  28. ///
  29. /// Class representing a MongoDB aggregation pipeline.
  30. ///
  31. class MONGOCXX_API pipeline {
  32. public:
  33. ///
  34. /// Creates a new aggregation pipeline.
  35. ///
  36. /// @see https://docs.mongodb.com/master/core/aggregation-pipeline/
  37. ///
  38. pipeline();
  39. ///
  40. /// Move constructs a pipeline.
  41. ///
  42. pipeline(pipeline&&) noexcept;
  43. ///
  44. /// Move assigns a pipeline.
  45. ///
  46. pipeline& operator=(pipeline&&) noexcept;
  47. ///
  48. /// Destroys a pipeline.
  49. ///
  50. ~pipeline();
  51. ///
  52. /// Adds new fields to documents.
  53. ///
  54. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/addFields/
  55. ///
  56. /// @param fields_to_add
  57. /// A document specifying the fields to add. For each field specified in this parameter, a
  58. /// corresponding field will be added to the documents, where the value of the added field is
  59. /// the result of evaluating the specified expression.
  60. ///
  61. /// @return
  62. /// A reference to the object on which this member function is being called. This facilitates
  63. /// method chaining.
  64. ///
  65. pipeline& add_fields(bsoncxx::document::view_or_value fields_to_add);
  66. ///
  67. /// Categorizes documents into groups, called buckets, based on a specified expression and
  68. /// bucket boundaries.
  69. ///
  70. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucket/
  71. ///
  72. /// @param bucket_args
  73. /// The specification for the bucket operation. The required fields `groupBy` and
  74. /// `boundaries` must be included.
  75. ///
  76. /// @return
  77. /// A reference to the object on which this member function is being called. This facilitates
  78. /// method chaining.
  79. ///
  80. pipeline& bucket(bsoncxx::document::view_or_value bucket_args);
  81. ///
  82. /// Categorizes documents into a specific number of groups, called buckets, based on a
  83. /// specified expression. Bucket boundaries are automatically determined in an attempt to
  84. /// evenly distribute the documents into the specified number of buckets.
  85. ///
  86. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/bucketAuto/
  87. ///
  88. /// @param bucket_auto_args
  89. /// The specification for the bucket_auto operation. This required fields `groupBy` and
  90. /// `buckets` must be included.
  91. ///
  92. /// @return
  93. /// A reference to the object on which this member function is being called. This facilitates
  94. /// method chaining.
  95. ///
  96. pipeline& bucket_auto(bsoncxx::document::view_or_value bucket_auto_args);
  97. ///
  98. /// Returns statistics regarding a collection or view.
  99. ///
  100. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/collStats/
  101. ///
  102. /// @param coll_stats_args
  103. /// The specification for the coll_stats operation. See link above for a list of valid
  104. /// options.
  105. ///
  106. /// @return
  107. /// A reference to the object on which this member function is being called. This facilitates
  108. /// method chaining.
  109. ///
  110. pipeline& coll_stats(
  111. bsoncxx::document::view_or_value coll_stats_args = bsoncxx::document::view{});
  112. ///
  113. /// Returns a document containing a count of the number of documents input to the stage.
  114. ///
  115. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/count/
  116. ///
  117. /// @param field
  118. /// Name of the field for the count to be written to.
  119. ///
  120. /// @return
  121. /// A reference to the object on which this member function is being called. This facilitates
  122. /// method chaining.
  123. ///
  124. pipeline& count(std::string field);
  125. ///
  126. /// Returns a stream of documents containing information on active and/or dormant
  127. /// operations as well as inactive sessions that are holding locks as part of a
  128. /// transaction. The stage returns a document for each operation or session.
  129. ///
  130. /// This stage must be used with database aggregate on the 'admin' database.
  131. ///
  132. /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/currentOp/
  133. ///
  134. /// @param current_op_args
  135. /// A document containing the arguments for the current_op operation.
  136. ///
  137. /// @return
  138. /// A reference to the object on which this method is being called.
  139. ///
  140. pipeline& current_op(bsoncxx::document::view_or_value current_op_args);
  141. ///
  142. /// Processes multiple aggregation pipelines within a single stage on the same set of input
  143. /// documents.
  144. ///
  145. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/facet/
  146. ///
  147. /// @param facet_args
  148. /// The specification for the facet operation. Each field in the the provided document should
  149. /// specify an aggregation pipeline, as an array.
  150. ///
  151. /// @return
  152. /// A reference to the object on which this member function is being called. This facilitates
  153. /// method chaining.
  154. ///
  155. pipeline& facet(bsoncxx::document::view_or_value facet_args);
  156. ///
  157. /// Appends a stage to this pipeline object.
  158. ///
  159. /// Use this method to run new pipeline stages that are not yet offered as
  160. /// explicit methods on this class. Pass in a document describing a
  161. /// pipeline stage:
  162. ///
  163. /// { "$addFields" : { "field" : 0 }}
  164. ///
  165. /// @param stage
  166. /// A bson document view or value describing a pipeline stage.
  167. ///
  168. /// @return
  169. /// A reference to this object on which this member function is being called.
  170. ///
  171. pipeline& append_stage(bsoncxx::document::view_or_value stage);
  172. ///
  173. /// Appends stages to this pipeline object from the given bson array.
  174. ///
  175. /// Use this method to run new pipeline stages that are not yet offered as
  176. /// explicit methods on this class. Pass in an array of documents, where each
  177. /// document in the array describes a pipeline stage:
  178. ///
  179. /// [ { "$addFields" : { "field" : 0 } }, { "$newStage" : ... }, ... ]
  180. ///
  181. /// @param stages
  182. /// A bson array containing documents describing pipeline stages.
  183. ///
  184. /// @return
  185. /// A reference to the object on which this member function is being called.
  186. ///
  187. pipeline& append_stages(bsoncxx::array::view_or_value stages);
  188. ///
  189. /// Outputs documents in order of nearest to farthest from a specified point.
  190. ///
  191. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/geoNear/
  192. ///
  193. /// @param geo_near_args
  194. /// The specification for the geo_near operation. The required fields `near` and
  195. /// `distanceField` must be included.
  196. ///
  197. /// @return
  198. /// A reference to the object on which this member function is being called. This facilitates
  199. /// method chaining.
  200. ///
  201. pipeline& geo_near(bsoncxx::document::view_or_value geo_near_args);
  202. ///
  203. /// Performs a recursive search on a collection.
  204. ///
  205. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/graphLookup/
  206. ///
  207. /// @param graph_lookup_args
  208. /// The specification for the graph_lookup operation. The required fields `from`,
  209. /// `connectFromField`, `startWith`, `connectToField`, and `as` must be included.
  210. ///
  211. /// @return
  212. /// A reference to the object on which this member function is being called. This facilitates
  213. /// method chaining.
  214. ///
  215. pipeline& graph_lookup(bsoncxx::document::view_or_value graph_lookup_args);
  216. ///
  217. /// Groups documents by some specified expression and outputs to the next stage a
  218. /// document for each distinct grouping. The output documents contain an `_id` field
  219. /// which contains the the distinct key for that group. The output documents can also
  220. /// contain computed fields that hold the values of some accumulator expression grouped
  221. /// by the group's `_id` field.
  222. ///
  223. /// @note group does not order output documents.
  224. ///
  225. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/group/
  226. ///
  227. /// @param group_args
  228. /// The specification for the group operation. The required field `_id` must be included.
  229. ///
  230. /// @return
  231. /// A reference to the object on which this member function is being called. This facilitates
  232. /// method chaining.
  233. ///
  234. pipeline& group(bsoncxx::document::view_or_value group_args);
  235. ///
  236. /// Returns statistics regarding the use of each index for the collection.
  237. ///
  238. /// @return
  239. /// A reference to the object on which this member function is being called. This facilitates
  240. /// method chaining.
  241. ///
  242. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/indexStats/
  243. ///
  244. pipeline& index_stats();
  245. ///
  246. /// Limits the number of documents passed to the next stage in the pipeline.
  247. ///
  248. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/limit/
  249. ///
  250. /// @param limit
  251. /// The number of documents to which output should be limited.
  252. ///
  253. /// @return
  254. /// A reference to the object on which this member function is being called. This facilitates
  255. /// method chaining.
  256. ///
  257. pipeline& limit(std::int32_t limit);
  258. ///
  259. /// Lists the sessions cached in memory by the mongod or mongos instance.
  260. ///
  261. /// This option must be used with database aggregate.
  262. ///
  263. /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/listLocalSessions/
  264. ///
  265. /// @param list_local_sessions_args
  266. /// A document containing the arguments for list_local_sessions.
  267. ///
  268. /// @return
  269. /// A reference to the object on which this method is being called.
  270. ///
  271. pipeline& list_local_sessions(bsoncxx::document::view_or_value list_local_sessions_args);
  272. ///
  273. /// Lists all sessions stored in the system.sessions collection in the config database.
  274. /// These sessions are visible to all members of the MongoDB deployment.
  275. ///
  276. /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/listSessions/
  277. ///
  278. /// @param list_sessions_args
  279. /// A document containing the arguments for list_sessions.
  280. ///
  281. /// @return
  282. /// A reference to the object on which this method is being called.
  283. ///
  284. pipeline& list_sessions(bsoncxx::document::view_or_value list_sessions_args);
  285. ///
  286. /// Performs a left outer join to an unsharded collection in the same database to filter in
  287. /// documents from the "joined" collection for processing.
  288. ///
  289. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/lookup/
  290. ///
  291. /// @param lookup_args
  292. /// The specification for the lookup operation. The required fields `from`, `localField`,
  293. /// `foreignField`, and `as` must be included.
  294. ///
  295. /// @return
  296. /// A reference to the object on which this member function is being called. This facilitates
  297. /// method chaining.
  298. ///
  299. pipeline& lookup(bsoncxx::document::view_or_value lookup_args);
  300. ///
  301. /// Filters the documents. Only the documents that match the condition(s) specified by the
  302. /// `filter` will continue to the next pipeline stage.
  303. ///
  304. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/match/
  305. ///
  306. /// @param filter
  307. /// The filter.
  308. ///
  309. /// @return
  310. /// A reference to the object on which this member function is being called. This facilitates
  311. /// method chaining.
  312. ///
  313. pipeline& match(bsoncxx::document::view_or_value filter);
  314. ///
  315. /// Outputs the aggregation results to a collection.
  316. ///
  317. /// @see https://docs.mongodb.com/manual/reference/operator/aggregation/merge/
  318. ///
  319. /// @param merge_args
  320. /// The specification for the merge options. Must include an `into` field that
  321. /// is either a collection name or a subdocument of the form:
  322. /// { `db` : <db name>, `coll` : <collection name> }.
  323. ///
  324. /// @return
  325. /// A reference to the object on which this member function is being called.
  326. ///
  327. pipeline& merge(bsoncxx::document::view_or_value merge_args);
  328. ///
  329. /// Takes documents returned by the aggregation pipeline and writes them to a specified
  330. /// collection. This stage must be the last stage in the pipeline. The out operator lets the
  331. /// aggregation framework return result sets of any size.
  332. ///
  333. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/out/
  334. ///
  335. /// @param collection_name
  336. /// The name of the collection where the output documents should go.
  337. ///
  338. /// @return
  339. /// A reference to the object on which this member function is being called. This facilitates
  340. /// method chaining.
  341. ///
  342. pipeline& out(std::string collection_name);
  343. ///
  344. /// Projects a subset of the fields in the documents to the next stage of the pipeline.
  345. ///
  346. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/project/
  347. ///
  348. /// @param projection
  349. /// The projection specification.
  350. ///
  351. /// @return
  352. /// A reference to the object on which this member function is being called. This facilitates
  353. /// method chaining.
  354. ///
  355. pipeline& project(bsoncxx::document::view_or_value projection);
  356. ///
  357. /// Restricts the contents of the documents based on information stored in the documents
  358. /// themselves.
  359. ///
  360. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/redact/
  361. ///
  362. /// @param restrictions
  363. /// The document restrictions.
  364. ///
  365. /// @return
  366. /// A reference to the object on which this member function is being called. This facilitates
  367. /// method chaining.
  368. ///
  369. pipeline& redact(bsoncxx::document::view_or_value restrictions);
  370. ///
  371. /// Promotes a specified document to the top level and replaces all other fields.
  372. ///
  373. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/replaceRoot/
  374. ///
  375. /// @param replace_root_args
  376. /// The specification for the replace_root operation. The required field `newRoot` must be
  377. /// included.
  378. ///
  379. /// @return
  380. /// A reference to the object on which this member function is being called. This facilitates
  381. /// method chaining.
  382. ///
  383. pipeline& replace_root(bsoncxx::document::view_or_value replace_root_args);
  384. ///
  385. /// Randomly selects the specified number of documents that pass into the stage and passes the
  386. /// remaining documents to the next stage in the pipeline.
  387. ///
  388. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sample/
  389. ///
  390. /// @param size
  391. /// The number of input documents to select.
  392. ///
  393. /// @return
  394. /// A reference to the object on which this member function is being called. This facilitates
  395. /// method chaining.
  396. ///
  397. pipeline& sample(std::int32_t size);
  398. ///
  399. /// Skips over the specified number of documents that pass into the stage and passes the
  400. /// remaining documents to the next stage in the pipeline.
  401. ///
  402. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/skip/
  403. ///
  404. /// @param docs_to_skip
  405. /// The number of input documents to skip.
  406. ///
  407. /// @return
  408. /// A reference to the object on which this member function is being called. This facilitates
  409. /// method chaining.
  410. ///
  411. pipeline& skip(std::int32_t docs_to_skip);
  412. ///
  413. /// Sorts all input documents and returns them to the pipeline in sorted order.
  414. ///
  415. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sort/
  416. ///
  417. /// @param ordering
  418. /// Document specifying the ordering by which the documents are sorted.
  419. ///
  420. /// @return
  421. /// A reference to the object on which this member function is being called. This facilitates
  422. /// method chaining.
  423. ///
  424. pipeline& sort(bsoncxx::document::view_or_value ordering);
  425. ///
  426. /// Groups incoming documents based on the value of a specified expression, then computes the
  427. /// count of documents in each distinct group.
  428. ///
  429. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
  430. ///
  431. /// @param field_expression
  432. /// The expression to group by, as an object. The expression can not evaluate to an object.
  433. ///
  434. /// @return
  435. /// A reference to the object on which this member function is being called. This facilitates
  436. /// method chaining.
  437. ///
  438. /// @note
  439. /// This overload of sort_by_count() is intended to be used when the desired sort is over a
  440. /// grouping of the result of a complex expression computed from the input documents.
  441. ///
  442. pipeline& sort_by_count(bsoncxx::document::view_or_value field_expression);
  443. ///
  444. /// Groups incoming documents based on the value of a specified expression, then computes the
  445. /// count of documents in each distinct group.
  446. ///
  447. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/sortByCount/
  448. ///
  449. /// @param field_expression
  450. /// The expression to group by, as a string. To specify a field path, prefix the field path
  451. /// with a dollar sign (`$`).
  452. ///
  453. /// @return
  454. /// A reference to the object on which this member function is being called. This facilitates
  455. /// method chaining.
  456. ///
  457. /// @note
  458. /// This overload of sort_by_count() is intended to be used when the desired sort is over a
  459. /// grouping of the value of a particular element in the input documents.
  460. ///
  461. pipeline& sort_by_count(std::string field_expression);
  462. ///
  463. /// Deconstructs an array field from the input documents to output a document for each element.
  464. /// Each output document is an input document with the value of its array field replaced by
  465. /// an element from the unwound array.
  466. ///
  467. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/unwind/
  468. ///
  469. /// @param unwind_args
  470. /// The specification for the unwind operation. The required field path must be included.
  471. ///
  472. /// @return
  473. /// A reference to the object on which this member function is being called. This facilitates
  474. /// method chaining.
  475. ///
  476. /// @note
  477. /// This overload of unwind() is intended to be used when additional options other than the
  478. /// field name need to be specified.
  479. ///
  480. pipeline& unwind(bsoncxx::document::view_or_value unwind_args);
  481. ///
  482. /// Deconstructs an array field from the input documents to output a document for each element.
  483. /// Each output document is an input document with the value of its array field replaced by
  484. /// an element from the unwound array.
  485. ///
  486. /// @see https://docs.mongodb.com/master/reference/operator/aggregation/unwind/
  487. ///
  488. /// @param field_name
  489. /// The name of the field to unwind.
  490. ///
  491. /// @return
  492. /// A reference to the object on which this member function is being called. This facilitates
  493. /// method chaining.
  494. ///
  495. /// @note
  496. /// This overload of unwind() is intended to be used when no options other than the field name
  497. /// need to be specified.
  498. ///
  499. pipeline& unwind(std::string field_name);
  500. ///
  501. /// @return A view of the underlying BSON array this pipeline represents.
  502. ///
  503. bsoncxx::array::view view_array() const;
  504. private:
  505. friend class client;
  506. friend class collection;
  507. friend class database;
  508. class MONGOCXX_PRIVATE impl;
  509. std::unique_ptr<impl> _impl;
  510. };
  511. MONGOCXX_INLINE_NAMESPACE_END
  512. } // namespace mongocxx
  513. #include <mongocxx/config/postlude.hpp>