collection.hpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. // Copyright 2014-present 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 <algorithm>
  16. #include <bsoncxx/builder/basic/array.hpp>
  17. #include <bsoncxx/builder/basic/document.hpp>
  18. #include <bsoncxx/builder/basic/kvp.hpp>
  19. #include <bsoncxx/builder/concatenate.hpp>
  20. #include <bsoncxx/document/view_or_value.hpp>
  21. #include <bsoncxx/oid.hpp>
  22. #include <bsoncxx/stdx/optional.hpp>
  23. #include <bsoncxx/string/view_or_value.hpp>
  24. #include <mongocxx/bulk_write.hpp>
  25. #include <mongocxx/change_stream.hpp>
  26. #include <mongocxx/client_session.hpp>
  27. #include <mongocxx/config/prelude.hpp>
  28. #include <mongocxx/cursor.hpp>
  29. #include <mongocxx/index_view.hpp>
  30. #include <mongocxx/model/insert_one.hpp>
  31. #include <mongocxx/options/aggregate.hpp>
  32. #include <mongocxx/options/bulk_write.hpp>
  33. #include <mongocxx/options/change_stream.hpp>
  34. #include <mongocxx/options/count.hpp>
  35. #include <mongocxx/options/delete.hpp>
  36. #include <mongocxx/options/distinct.hpp>
  37. #include <mongocxx/options/estimated_document_count.hpp>
  38. #include <mongocxx/options/find.hpp>
  39. #include <mongocxx/options/find_one_and_delete.hpp>
  40. #include <mongocxx/options/find_one_and_replace.hpp>
  41. #include <mongocxx/options/find_one_and_update.hpp>
  42. #include <mongocxx/options/index.hpp>
  43. #include <mongocxx/options/index_view.hpp>
  44. #include <mongocxx/options/insert.hpp>
  45. #include <mongocxx/options/replace.hpp>
  46. #include <mongocxx/options/update.hpp>
  47. #include <mongocxx/pipeline.hpp>
  48. #include <mongocxx/read_concern.hpp>
  49. #include <mongocxx/read_preference.hpp>
  50. #include <mongocxx/result/bulk_write.hpp>
  51. #include <mongocxx/result/delete.hpp>
  52. #include <mongocxx/result/insert_many.hpp>
  53. #include <mongocxx/result/insert_one.hpp>
  54. #include <mongocxx/result/replace_one.hpp>
  55. #include <mongocxx/result/update.hpp>
  56. #include <mongocxx/write_concern.hpp>
  57. #include <string>
  58. namespace mongocxx {
  59. MONGOCXX_INLINE_NAMESPACE_BEGIN
  60. class client;
  61. class database;
  62. ///
  63. /// Class representing server side document groupings within a MongoDB database.
  64. ///
  65. /// Collections do not require or enforce a schema and documents inside of a collection can have
  66. /// different fields. While not a requirement, typically documents in a collection have a similar
  67. /// shape or related purpose.
  68. ///
  69. /// Example:
  70. /// @code
  71. /// // Connect and get a collection.
  72. /// mongocxx::client mongo_client{mongocxx::uri{}};
  73. /// auto coll = mongo_client["database"]["collection"];
  74. /// @endcode
  75. ///
  76. class MONGOCXX_API collection {
  77. //
  78. // Utility class supporting the convenience of {} meaning an empty bsoncxx::document.
  79. //
  80. // Users may not use this class directly.
  81. //
  82. // In places where driver methods take this class as a parameter, passing {} will
  83. // translate to a default-constructed bsoncxx::document::view_or_value,
  84. // regardless of other overloads taking other default-constructible types
  85. // for that parameter. This class avoids compiler ambiguity with such overloads.
  86. //
  87. // See collection::update_one for an example of such overloads.
  88. //
  89. class _empty_doc_tag {
  90. _empty_doc_tag() = default;
  91. };
  92. public:
  93. ///
  94. /// Default constructs a collection object. The collection is
  95. /// equivalent to the state of a moved from collection. The only
  96. /// valid actions to take with a default constructed collection
  97. /// are to assign to it, or destroy it.
  98. ///
  99. collection() noexcept;
  100. ///
  101. /// Move constructs a collection.
  102. ///
  103. collection(collection&&) noexcept;
  104. ///
  105. /// Move assigns a collection.
  106. ///
  107. collection& operator=(collection&&) noexcept;
  108. ///
  109. /// Copy constructs a collection.
  110. ///
  111. collection(const collection&);
  112. ///
  113. /// Copy assigns a collection.
  114. ///
  115. collection& operator=(const collection&);
  116. ///
  117. /// Destroys a collection.
  118. ///
  119. ~collection();
  120. ///
  121. /// Returns true if the collection is valid, meaning it was not
  122. /// default constructed or moved from.
  123. ///
  124. explicit operator bool() const noexcept;
  125. ///
  126. /// @{
  127. ///
  128. /// Runs an aggregation framework pipeline against this collection.
  129. ///
  130. /// @param pipeline
  131. /// The pipeline of aggregation operations to perform.
  132. /// @param options
  133. /// Optional arguments, see mongocxx::options::aggregate.
  134. ///
  135. /// @return A mongocxx::cursor with the results. If the query fails,
  136. /// the cursor throws mongocxx::query_exception when the returned cursor
  137. /// is iterated.
  138. ///
  139. /// @see https://docs.mongodb.com/master/reference/command/aggregate/
  140. ///
  141. /// @note
  142. /// In order to pass a read concern to this, you must use the
  143. /// collection level set read concern - collection::read_concern(rc).
  144. /// (Write concern supported only for MongoDB 3.4+).
  145. ///
  146. cursor aggregate(const pipeline& pipeline,
  147. const options::aggregate& options = options::aggregate());
  148. ///
  149. /// Runs an aggregation framework pipeline against this collection.
  150. ///
  151. /// @param session
  152. /// The mongocxx::client_session with which to perform the aggregation.
  153. /// @param pipeline
  154. /// The pipeline of aggregation operations to perform.
  155. /// @param options
  156. /// Optional arguments, see mongocxx::options::aggregate.
  157. ///
  158. /// @return A mongocxx::cursor with the results. If the query fails,
  159. /// the cursor throws mongocxx::query_exception when the returned cursor
  160. /// is iterated.
  161. ///
  162. /// @see https://docs.mongodb.com/master/reference/command/aggregate/
  163. ///
  164. /// @note
  165. /// In order to pass a read concern to this, you must use the
  166. /// collection level set read concern - collection::read_concern(rc).
  167. /// (Write concern supported only for MongoDB 3.4+).
  168. ///
  169. cursor aggregate(const client_session& session,
  170. const pipeline& pipeline,
  171. const options::aggregate& options = options::aggregate());
  172. ///
  173. /// @}
  174. ///
  175. ///
  176. /// @{
  177. ///
  178. /// Creates a new bulk operation to be executed against this collection.
  179. /// The lifetime of the bulk_write is independent of the collection.
  180. ///
  181. /// @param options
  182. /// Optional arguments; see mongocxx::options::bulk_write.
  183. ///
  184. /// @return
  185. /// The newly-created bulk write.
  186. ///
  187. class bulk_write create_bulk_write(const options::bulk_write& options = {});
  188. ///
  189. /// Creates a new bulk operation to be executed against this collection.
  190. /// The lifetime of the bulk_write is independent of the collection.
  191. ///
  192. /// @param session
  193. /// The mongocxx::client_session with which to perform the bulk operation.
  194. /// @param options
  195. /// Optional arguments; see mongocxx::options::bulk_write.
  196. ///
  197. /// @return
  198. /// The newly-created bulk write.
  199. ///
  200. class bulk_write create_bulk_write(const client_session& session,
  201. const options::bulk_write& options = {});
  202. ///
  203. /// @}
  204. ///
  205. ///
  206. /// @{
  207. ///
  208. /// Sends a write to the server as a bulk write operation.
  209. ///
  210. /// @param write
  211. /// A model::write.
  212. /// @param options
  213. /// Optional arguments, see options::bulk_write.
  214. ///
  215. /// @return
  216. /// The optional result of the bulk operation execution.
  217. /// If the write concern is unacknowledged, the optional will be
  218. /// disengaged.
  219. ///
  220. /// @exception
  221. /// mongocxx::bulk_write_exception when there are errors processing
  222. /// the writes.
  223. ///
  224. /// @see mongocxx::bulk_write
  225. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  226. ///
  227. MONGOCXX_INLINE stdx::optional<result::bulk_write> write(
  228. const model::write& write, const options::bulk_write& options = options::bulk_write());
  229. ///
  230. /// Sends a write to the server as a bulk write operation.
  231. ///
  232. /// @param session
  233. /// The mongocxx::client_session with which to perform the bulk operation.
  234. /// @param write
  235. /// A model::write.
  236. /// @param options
  237. /// Optional arguments, see options::bulk_write.
  238. ///
  239. /// @return
  240. /// The optional result of the bulk operation execution.
  241. /// If the write concern is unacknowledged, the optional will be
  242. /// disengaged.
  243. ///
  244. /// @exception
  245. /// mongocxx::bulk_write_exception when there are errors processing
  246. /// the writes.
  247. ///
  248. /// @see mongocxx::bulk_write
  249. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  250. ///
  251. MONGOCXX_INLINE stdx::optional<result::bulk_write> write(
  252. const client_session& session,
  253. const model::write& write,
  254. const options::bulk_write& options = options::bulk_write());
  255. ///
  256. /// @}
  257. ///
  258. ///
  259. /// @{
  260. ///
  261. /// Sends a container of writes to the server as a bulk write operation.
  262. ///
  263. /// @tparam container_type
  264. /// The container type. Must meet the requirements for the container concept with a value
  265. /// type of model::write.
  266. ///
  267. /// @param writes
  268. /// A container of model::write.
  269. /// @param options
  270. /// Optional arguments, see options::bulk_write.
  271. ///
  272. /// @return The optional result of the bulk operation execution.
  273. /// If the write concern is unacknowledged, the optional will be
  274. /// disengaged.
  275. ///
  276. /// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
  277. ///
  278. /// @see mongocxx::bulk_write
  279. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  280. ///
  281. template <typename container_type>
  282. MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
  283. const container_type& writes, const options::bulk_write& options = options::bulk_write());
  284. ///
  285. /// Sends a container of writes to the server as a bulk write operation.
  286. ///
  287. /// @tparam container_type
  288. /// The container type. Must meet the requirements for the container concept with a value
  289. /// type of model::write.
  290. ///
  291. /// @param session
  292. /// The mongocxx::client_session with which to perform the bulk operation.
  293. /// @param writes
  294. /// A container of model::write.
  295. /// @param options
  296. /// Optional arguments, see options::bulk_write.
  297. ///
  298. /// @return The optional result of the bulk operation execution.
  299. /// If the write concern is unacknowledged, the optional will be
  300. /// disengaged.
  301. ///
  302. /// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
  303. ///
  304. /// @see mongocxx::bulk_write
  305. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  306. ///
  307. template <typename container_type>
  308. MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
  309. const client_session& session,
  310. const container_type& writes,
  311. const options::bulk_write& options = options::bulk_write());
  312. ///
  313. /// @}
  314. ///
  315. ///
  316. /// @{
  317. ///
  318. /// Sends writes starting at @c begin and ending at @c end to the server as a bulk write
  319. /// operation.
  320. ///
  321. /// @tparam write_model_iterator_type
  322. /// The container type. Must meet the requirements for the input iterator concept with a value
  323. /// type of model::write.
  324. ///
  325. /// @param begin
  326. /// Iterator pointing to the first model::write to send.
  327. /// @param end
  328. /// Iterator pointing to the end of the writes to send.
  329. /// @param options
  330. /// Optional arguments, see options::bulk_write.
  331. ///
  332. /// @return The optional result of the bulk operation execution, a result::bulk_write.
  333. ///
  334. /// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
  335. ///
  336. /// @see mongocxx::bulk_write
  337. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  338. ///
  339. template <typename write_model_iterator_type>
  340. MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
  341. write_model_iterator_type begin,
  342. write_model_iterator_type end,
  343. const options::bulk_write& options = options::bulk_write());
  344. ///
  345. /// Sends writes starting at @c begin and ending at @c end to the server as a bulk write
  346. /// operation.
  347. ///
  348. /// @tparam write_model_iterator_type
  349. /// The container type. Must meet the requirements for the input iterator concept with a value
  350. /// type of model::write.
  351. ///
  352. /// @param session
  353. /// The mongocxx::client_session with which to perform the bulk operation.
  354. /// @param begin
  355. /// Iterator pointing to the first model::write to send.
  356. /// @param end
  357. /// Iterator pointing to the end of the writes to send.
  358. /// @param options
  359. /// Optional arguments, see options::bulk_write.
  360. ///
  361. /// @return The optional result of the bulk operation execution, a result::bulk_write.
  362. ///
  363. /// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
  364. ///
  365. /// @see mongocxx::bulk_write
  366. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  367. ///
  368. template <typename write_model_iterator_type>
  369. MONGOCXX_INLINE stdx::optional<result::bulk_write> bulk_write(
  370. const client_session& session,
  371. write_model_iterator_type begin,
  372. write_model_iterator_type end,
  373. const options::bulk_write& options = options::bulk_write());
  374. ///
  375. /// @}
  376. ///
  377. ///
  378. /// @{
  379. ///
  380. /// Counts the number of documents matching the provided filter.
  381. ///
  382. /// @param filter
  383. /// The filter that documents must match in order to be counted.
  384. /// @param options
  385. /// Optional arguments, see mongocxx::options::count.
  386. ///
  387. /// @return The count of the documents that matched the filter.
  388. ///
  389. /// @throws mongocxx::query_exception if the count operation fails.
  390. ///
  391. std::int64_t count_documents(bsoncxx::document::view_or_value filter,
  392. const options::count& options = options::count());
  393. ///
  394. /// Counts the number of documents matching the provided filter.
  395. ///
  396. /// @param session
  397. /// The mongocxx::client_session with which to perform the count.
  398. /// @param filter
  399. /// The filter that documents must match in order to be counted.
  400. /// @param options
  401. /// Optional arguments, see mongocxx::options::count.
  402. ///
  403. /// @return The count of the documents that matched the filter.
  404. ///
  405. /// @throws mongocxx::query_exception if the count operation fails.
  406. ///
  407. std::int64_t count_documents(const client_session& session,
  408. bsoncxx::document::view_or_value filter,
  409. const options::count& options = options::count());
  410. ///
  411. /// @}
  412. ///
  413. ///
  414. /// @{
  415. ///
  416. /// Returns an estimate of the number of documents in the collection.
  417. ///
  418. /// @param options
  419. /// Optional arguments, see mongocxx::options::count.
  420. ///
  421. /// @return The count of the documents that matched the filter.
  422. ///
  423. /// @throws mongocxx::query_exception if the count operation fails.
  424. ///
  425. std::int64_t estimated_document_count(
  426. const options::estimated_document_count& options = options::estimated_document_count());
  427. ///
  428. /// @}
  429. ///
  430. ///
  431. /// @{
  432. ///
  433. /// Creates an index over the collection for the provided keys with the provided options.
  434. ///
  435. /// @param keys
  436. /// The keys for the index: @c {a: 1, b: -1}
  437. /// @param index_options
  438. /// A document containing optional arguments for creating the index.
  439. /// @param operation_options
  440. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  441. ///
  442. /// @exception
  443. /// mongocxx::operation_exception if index creation fails.
  444. ///
  445. /// @see
  446. /// https://docs.mongodb.com/master/reference/command/createIndexes/
  447. ///
  448. /// @note
  449. /// Write concern supported only for MongoDB 3.4+.
  450. ///
  451. bsoncxx::document::value create_index(
  452. bsoncxx::document::view_or_value keys,
  453. bsoncxx::document::view_or_value index_options = {},
  454. options::index_view operation_options = options::index_view{});
  455. ///
  456. /// Creates an index over the collection for the provided keys with the provided options.
  457. ///
  458. /// @param session
  459. /// The mongocxx::client_session with which to perform the index creation.
  460. /// @param keys
  461. /// The keys for the index: @c {a: 1, b: -1}
  462. /// @param index_options
  463. /// A document containing optional arguments for creating the index.
  464. /// @param operation_options
  465. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  466. ///
  467. /// @exception
  468. /// mongocxx::operation_exception if index creation fails.
  469. ///
  470. /// @see
  471. /// https://docs.mongodb.com/master/reference/command/createIndexes/
  472. ///
  473. /// @note
  474. /// Write concern supported only for MongoDB 3.4+.
  475. ///
  476. bsoncxx::document::value create_index(
  477. const client_session& session,
  478. bsoncxx::document::view_or_value keys,
  479. bsoncxx::document::view_or_value index_options = {},
  480. options::index_view operation_options = options::index_view{});
  481. ///
  482. /// @}
  483. ///
  484. ///
  485. /// @{
  486. ///
  487. /// Deletes all matching documents from the collection.
  488. ///
  489. /// @param filter
  490. /// Document view representing the data to be deleted.
  491. /// @param options
  492. /// Optional arguments, see mongocxx::options::delete_options.
  493. ///
  494. /// @return The optional result of performing the deletion.
  495. /// If the write concern is unacknowledged, the optional will be
  496. /// disengaged.
  497. ///
  498. /// @throws mongocxx::bulk_write_exception if the delete fails.
  499. ///
  500. /// @see https://docs.mongodb.com/master/reference/command/delete/
  501. ///
  502. stdx::optional<result::delete_result> delete_many(
  503. bsoncxx::document::view_or_value filter,
  504. const options::delete_options& options = options::delete_options());
  505. ///
  506. /// Deletes all matching documents from the collection.
  507. ///
  508. /// @param session
  509. /// The mongocxx::client_session with which to perform the deletion.
  510. /// @param filter
  511. /// Document view representing the data to be deleted.
  512. /// @param options
  513. /// Optional arguments, see mongocxx::options::delete_options.
  514. ///
  515. /// @return The optional result of performing the deletion.
  516. /// If the write concern is unacknowledged, the optional will be
  517. /// disengaged.
  518. ///
  519. /// @throws mongocxx::bulk_write_exception if the delete fails.
  520. ///
  521. /// @see https://docs.mongodb.com/master/reference/command/delete/
  522. ///
  523. stdx::optional<result::delete_result> delete_many(
  524. const client_session& session,
  525. bsoncxx::document::view_or_value filter,
  526. const options::delete_options& options = options::delete_options());
  527. ///
  528. /// @}
  529. ///
  530. ///
  531. /// @{
  532. ///
  533. /// Deletes a single matching document from the collection.
  534. ///
  535. /// @param filter
  536. /// Document view representing the data to be deleted.
  537. /// @param options
  538. /// Optional arguments, see mongocxx::options::delete_options.
  539. ///
  540. /// @return The optional result of performing the deletion.
  541. /// If the write concern is unacknowledged, the optional will be
  542. /// disengaged.
  543. ///
  544. /// @throws mongocxx::bulk_write_exception if the delete fails.
  545. ///
  546. /// @see https://docs.mongodb.com/master/reference/command/delete/
  547. ///
  548. stdx::optional<result::delete_result> delete_one(
  549. bsoncxx::document::view_or_value filter,
  550. const options::delete_options& options = options::delete_options());
  551. ///
  552. /// Deletes a single matching document from the collection.
  553. ///
  554. /// @param session
  555. /// The mongocxx::client_session with which to perform the deletion.
  556. /// @param filter
  557. /// Document view representing the data to be deleted.
  558. /// @param options
  559. /// Optional arguments, see mongocxx::options::delete_options.
  560. ///
  561. /// @return The optional result of performing the deletion.
  562. /// If the write concern is unacknowledged, the optional will be
  563. /// disengaged.
  564. ///
  565. /// @throws mongocxx::bulk_write_exception if the delete fails.
  566. ///
  567. /// @see https://docs.mongodb.com/master/reference/command/delete/
  568. ///
  569. stdx::optional<result::delete_result> delete_one(
  570. const client_session& session,
  571. bsoncxx::document::view_or_value filter,
  572. const options::delete_options& options = options::delete_options());
  573. ///
  574. /// @}
  575. ///
  576. ///
  577. /// @{
  578. ///
  579. /// Finds the distinct values for a specified field across the collection.
  580. ///
  581. /// @param name
  582. /// The field for which the distinct values will be found.
  583. /// @param filter
  584. /// Document view representing the documents for which the distinct operation will apply.
  585. /// @param options
  586. /// Optional arguments, see options::distinct.
  587. /// @return mongocxx::cursor having the distinct values for the specified
  588. /// field. If the operation fails, the cursor throws
  589. /// mongocxx::query_exception when the returned cursor is iterated.
  590. /// @see https://docs.mongodb.com/master/reference/command/distinct/
  591. ///
  592. cursor distinct(bsoncxx::string::view_or_value name,
  593. bsoncxx::document::view_or_value filter,
  594. const options::distinct& options = options::distinct());
  595. ///
  596. /// Finds the distinct values for a specified field across the collection.
  597. ///
  598. /// @param session
  599. /// The mongocxx::client_session with which to perform the operation.
  600. /// @param name
  601. /// The field for which the distinct values will be found.
  602. /// @param filter
  603. /// Document view representing the documents for which the distinct operation will apply.
  604. /// @param options
  605. /// Optional arguments, see options::distinct.
  606. /// @return mongocxx::cursor having the distinct values for the specified
  607. /// field. If the operation fails, the cursor throws
  608. /// mongocxx::query_exception when the returned cursor is iterated.
  609. /// @see https://docs.mongodb.com/master/reference/command/distinct/
  610. ///
  611. cursor distinct(const client_session& session,
  612. bsoncxx::string::view_or_value name,
  613. bsoncxx::document::view_or_value filter,
  614. const options::distinct& options = options::distinct());
  615. ///
  616. /// @}
  617. ///
  618. ///
  619. /// @{
  620. ///
  621. /// Drops this collection and all its contained documents from the database.
  622. ///
  623. /// @param write_concern (optional)
  624. /// The write concern to use for this operation. Defaults to the collection wide write
  625. /// concern if none is provided.
  626. ///
  627. /// @exception
  628. /// mongocxx::operation_exception if the operation fails.
  629. ///
  630. /// @see
  631. /// https://docs.mongodb.com/master/reference/command/drop/
  632. ///
  633. /// @note
  634. /// Write concern supported only for MongoDB 3.4+.
  635. ///
  636. void drop(const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
  637. ///
  638. /// Drops this collection and all its contained documents from the database.
  639. ///
  640. /// @param session
  641. /// The mongocxx::client_session with which to perform the drop.
  642. /// @param write_concern (optional)
  643. /// The write concern to use for this operation. Defaults to the collection wide write
  644. /// concern if none is provided.
  645. ///
  646. /// @exception
  647. /// mongocxx::operation_exception if the operation fails.
  648. ///
  649. /// @see
  650. /// https://docs.mongodb.com/master/reference/command/drop/
  651. ///
  652. /// @note
  653. /// Write concern supported only for MongoDB 3.4+.
  654. ///
  655. void drop(const client_session& session,
  656. const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
  657. ///
  658. /// @}
  659. ///
  660. ///
  661. /// @{
  662. ///
  663. /// Finds the documents in this collection which match the provided filter.
  664. ///
  665. /// @param filter
  666. /// Document view representing a document that should match the query.
  667. /// @param options
  668. /// Optional arguments, see options::find
  669. ///
  670. /// @return A mongocxx::cursor with the results. If the query fails,
  671. /// the cursor throws mongocxx::query_exception when the returned cursor
  672. /// is iterated.
  673. ///
  674. /// @throws mongocxx::logic_error if the options are invalid, or if the unsupported option
  675. /// modifiers "$query" or "$explain" are used.
  676. ///
  677. /// @see https://docs.mongodb.com/master/core/read-operations-introduction/
  678. ///
  679. cursor find(bsoncxx::document::view_or_value filter,
  680. const options::find& options = options::find());
  681. ///
  682. /// Finds the documents in this collection which match the provided filter.
  683. ///
  684. /// @param session
  685. /// The mongocxx::client_session with which to perform the query.
  686. /// @param filter
  687. /// Document view representing a document that should match the query.
  688. /// @param options
  689. /// Optional arguments, see options::find
  690. ///
  691. /// @return A mongocxx::cursor with the results. If the query fails,
  692. /// the cursor throws mongocxx::query_exception when the returned cursor
  693. /// is iterated.
  694. ///
  695. /// @throws mongocxx::logic_error if the options are invalid, or if the unsupported option
  696. /// modifiers "$query" or "$explain" are used.
  697. ///
  698. /// @see https://docs.mongodb.com/master/core/read-operations-introduction/
  699. ///
  700. cursor find(const client_session& session,
  701. bsoncxx::document::view_or_value filter,
  702. const options::find& options = options::find());
  703. ///
  704. /// @{
  705. ///
  706. /// Finds a single document in this collection that match the provided filter.
  707. ///
  708. /// @param filter
  709. /// Document view representing a document that should match the query.
  710. /// @param options
  711. /// Optional arguments, see options::find
  712. ///
  713. /// @return An optional document that matched the filter.
  714. ///
  715. /// @throws mongocxx::query_exception if the operation fails.
  716. ///
  717. /// @see https://docs.mongodb.com/master/core/read-operations-introduction/
  718. ///
  719. stdx::optional<bsoncxx::document::value> find_one(
  720. bsoncxx::document::view_or_value filter, const options::find& options = options::find());
  721. ///
  722. /// Finds a single document in this collection that match the provided filter.
  723. ///
  724. /// @param session
  725. /// The mongocxx::client_session with which to perform the query.
  726. /// @param filter
  727. /// Document view representing a document that should match the query.
  728. /// @param options
  729. /// Optional arguments, see options::find
  730. ///
  731. /// @return An optional document that matched the filter.
  732. ///
  733. /// @throws mongocxx::query_exception if the operation fails.
  734. ///
  735. /// @see https://docs.mongodb.com/master/core/read-operations-introduction/
  736. ///
  737. stdx::optional<bsoncxx::document::value> find_one(
  738. const client_session& session,
  739. bsoncxx::document::view_or_value filter,
  740. const options::find& options = options::find());
  741. ///
  742. /// @}
  743. ///
  744. ///
  745. /// @{
  746. ///
  747. /// Finds a single document matching the filter, deletes it, and returns the original.
  748. ///
  749. /// @param filter
  750. /// Document view representing a document that should be deleted.
  751. /// @param options
  752. /// Optional arguments, see options::find_one_and_delete
  753. ///
  754. /// @return The document that was deleted.
  755. ///
  756. /// @exception
  757. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  758. /// write concern is used.
  759. ///
  760. /// @exception
  761. /// Throws mongocxx::write_exception if the operation fails.
  762. ///
  763. stdx::optional<bsoncxx::document::value> find_one_and_delete(
  764. bsoncxx::document::view_or_value filter,
  765. const options::find_one_and_delete& options = options::find_one_and_delete());
  766. ///
  767. /// Finds a single document matching the filter, deletes it, and returns the original.
  768. ///
  769. /// @param session
  770. /// The mongocxx::client_session with which to perform the operation.
  771. /// @param filter
  772. /// Document view representing a document that should be deleted.
  773. /// @param options
  774. /// Optional arguments, see options::find_one_and_delete
  775. ///
  776. /// @return The document that was deleted.
  777. ///
  778. /// @exception
  779. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  780. /// write concern is used.
  781. ///
  782. /// @exception
  783. /// Throws mongocxx::write_exception if the operation fails.
  784. ///
  785. stdx::optional<bsoncxx::document::value> find_one_and_delete(
  786. const client_session& session,
  787. bsoncxx::document::view_or_value filter,
  788. const options::find_one_and_delete& options = options::find_one_and_delete());
  789. ///
  790. /// @}
  791. ///
  792. ///
  793. /// @{
  794. ///
  795. /// Finds a single document matching the filter, replaces it, and returns either the original
  796. /// or the replacement document.
  797. ///
  798. /// @param filter
  799. /// Document view representing a document that should be replaced.
  800. /// @param replacement
  801. /// Document view representing the replacement for a matching document.
  802. /// @param options
  803. /// Optional arguments, see options::find_one_and_replace.
  804. ///
  805. /// @return The original or replaced document.
  806. ///
  807. /// @exception
  808. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  809. /// write concern is used.
  810. ///
  811. /// @exception
  812. /// Throws mongocxx::write_exception if the operation fails.
  813. ///
  814. stdx::optional<bsoncxx::document::value> find_one_and_replace(
  815. bsoncxx::document::view_or_value filter,
  816. bsoncxx::document::view_or_value replacement,
  817. const options::find_one_and_replace& options = options::find_one_and_replace());
  818. ///
  819. /// Finds a single document matching the filter, replaces it, and returns either the original
  820. /// or the replacement document.
  821. ///
  822. /// @param session
  823. /// The mongocxx::client_session with which to perform the operation.
  824. /// @param filter
  825. /// Document view representing a document that should be replaced.
  826. /// @param replacement
  827. /// Document view representing the replacement for a matching document.
  828. /// @param options
  829. /// Optional arguments, see options::find_one_and_replace.
  830. ///
  831. /// @return The original or replaced document.
  832. ///
  833. /// @exception
  834. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  835. /// write concern is used.
  836. ///
  837. /// @exception
  838. /// Throws mongocxx::write_exception if the operation fails.
  839. ///
  840. stdx::optional<bsoncxx::document::value> find_one_and_replace(
  841. const client_session& session,
  842. bsoncxx::document::view_or_value filter,
  843. bsoncxx::document::view_or_value replacement,
  844. const options::find_one_and_replace& options = options::find_one_and_replace());
  845. ///
  846. /// @}
  847. ///
  848. ///
  849. /// @{
  850. ///
  851. /// Finds a single document matching the filter, updates it, and returns either the original
  852. /// or the newly-updated document.
  853. ///
  854. /// @param filter
  855. /// Document view representing a document that should be updated.
  856. /// @param update
  857. /// Document view representing the update to apply to a matching document.
  858. /// @param options
  859. /// Optional arguments, see options::find_one_and_update.
  860. ///
  861. /// @return The original or updated document.
  862. ///
  863. /// @exception
  864. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  865. /// write concern is used.
  866. ///
  867. /// @exception
  868. /// Throws mongocxx::write_exception if the operation fails.
  869. ///
  870. stdx::optional<bsoncxx::document::value> find_one_and_update(
  871. bsoncxx::document::view_or_value filter,
  872. bsoncxx::document::view_or_value update,
  873. const options::find_one_and_update& options = options::find_one_and_update());
  874. ///
  875. /// Finds a single document matching the filter, updates it, and returns either the original
  876. /// or the newly-updated document.
  877. ///
  878. /// @param filter
  879. /// Document view representing a document that should be updated.
  880. /// @param update
  881. /// Pipeline representing the update to apply to a matching document.
  882. /// @param options
  883. /// Optional arguments, see options::find_one_and_update.
  884. ///
  885. /// @return The original or updated document.
  886. ///
  887. /// @exception
  888. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  889. /// write concern is used.
  890. ///
  891. /// @exception
  892. /// Throws mongocxx::write_exception if the operation fails.
  893. ///
  894. stdx::optional<bsoncxx::document::value> find_one_and_update(
  895. bsoncxx::document::view_or_value filter,
  896. const pipeline& update,
  897. const options::find_one_and_update& options = options::find_one_and_update());
  898. ///
  899. /// Finds a single document matching the filter, updates it, and returns either the original
  900. /// or the newly-updated document.
  901. ///
  902. /// @param filter
  903. /// Document view representing a document that should be updated.
  904. /// @param update
  905. /// Supports the empty update {}.
  906. /// @param options
  907. /// Optional arguments, see options::find_one_and_update.
  908. ///
  909. /// @return The original or updated document.
  910. ///
  911. /// @exception
  912. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  913. /// write concern is used.
  914. ///
  915. /// @exception
  916. /// Throws mongocxx::write_exception if the operation fails.
  917. ///
  918. stdx::optional<bsoncxx::document::value> find_one_and_update(
  919. bsoncxx::document::view_or_value filter,
  920. std::initializer_list<_empty_doc_tag> update,
  921. const options::find_one_and_update& options = options::find_one_and_update());
  922. ///
  923. /// Finds a single document matching the filter, updates it, and returns either the original
  924. /// or the newly-updated document.
  925. ///
  926. /// @param session
  927. /// The mongocxx::client_session with which to perform the operation.
  928. /// @param filter
  929. /// Document view representing a document that should be updated.
  930. /// @param update
  931. /// Document view representing the update to apply to a matching document.
  932. /// @param options
  933. /// Optional arguments, see options::find_one_and_update.
  934. ///
  935. /// @return The original or updated document.
  936. ///
  937. /// @exception
  938. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  939. /// write concern is used.
  940. ///
  941. /// @exception
  942. /// Throws mongocxx::write_exception if the operation fails.
  943. ///
  944. stdx::optional<bsoncxx::document::value> find_one_and_update(
  945. const client_session& session,
  946. bsoncxx::document::view_or_value filter,
  947. bsoncxx::document::view_or_value update,
  948. const options::find_one_and_update& options = options::find_one_and_update());
  949. ///
  950. /// Finds a single document matching the filter, updates it, and returns either the original
  951. /// or the newly-updated document.
  952. ///
  953. /// @param session
  954. /// The mongocxx::client_session with which to perform the operation.
  955. /// @param filter
  956. /// Document view representing a document that should be updated.
  957. /// @param update
  958. /// Pipeline representing the update to apply to a matching document.
  959. /// @param options
  960. /// Optional arguments, see options::find_one_and_update.
  961. ///
  962. /// @return The original or updated document.
  963. ///
  964. /// @exception
  965. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  966. /// write concern is used.
  967. ///
  968. /// @exception
  969. /// Throws mongocxx::write_exception if the operation fails.
  970. ///
  971. stdx::optional<bsoncxx::document::value> find_one_and_update(
  972. const client_session& session,
  973. bsoncxx::document::view_or_value filter,
  974. const pipeline& update,
  975. const options::find_one_and_update& options = options::find_one_and_update());
  976. ///
  977. /// Finds a single document matching the filter, updates it, and returns either the original
  978. /// or the newly-updated document.
  979. ///
  980. /// @param session
  981. /// The mongocxx::client_session with which to perform the operation.
  982. /// @param filter
  983. /// Document view representing a document that should be updated.
  984. /// @param update
  985. /// Supports the empty update {}.
  986. /// @param options
  987. /// Optional arguments, see options::find_one_and_update.
  988. ///
  989. /// @return The original or updated document.
  990. ///
  991. /// @exception
  992. /// Throws mongocxx::logic_error if the collation option is specified and an unacknowledged
  993. /// write concern is used.
  994. ///
  995. /// @exception
  996. /// Throws mongocxx::write_exception if the operation fails.
  997. ///
  998. stdx::optional<bsoncxx::document::value> find_one_and_update(
  999. const client_session& session,
  1000. bsoncxx::document::view_or_value filter,
  1001. std::initializer_list<_empty_doc_tag> update,
  1002. const options::find_one_and_update& options = options::find_one_and_update());
  1003. ///
  1004. /// @}
  1005. ///
  1006. ///
  1007. /// @{
  1008. ///
  1009. /// Inserts a single document into the collection. If the document is missing an identifier
  1010. /// (@c _id field) one will be generated for it.
  1011. ///
  1012. /// @param document
  1013. /// The document to insert.
  1014. /// @param options
  1015. /// Optional arguments, see options::insert.
  1016. ///
  1017. /// @return The optional result of attempting to perform the insert.
  1018. /// If the write concern is unacknowledged, the optional will be
  1019. /// disengaged.
  1020. ///
  1021. /// @throws mongocxx::bulk_write_exception if the operation fails.
  1022. stdx::optional<result::insert_one> insert_one(bsoncxx::document::view_or_value document,
  1023. const options::insert& options = {});
  1024. ///
  1025. /// Inserts a single document into the collection. If the document is missing an identifier
  1026. /// (@c _id field) one will be generated for it.
  1027. ///
  1028. /// @param session
  1029. /// The mongocxx::client_session with which to perform the insert.
  1030. /// @param document
  1031. /// The document to insert.
  1032. /// @param options
  1033. /// Optional arguments, see options::insert.
  1034. ///
  1035. /// @return The optional result of attempting to perform the insert.
  1036. /// If the write concern is unacknowledged, the optional will be
  1037. /// disengaged.
  1038. ///
  1039. /// @throws mongocxx::bulk_write_exception if the operation fails.
  1040. stdx::optional<result::insert_one> insert_one(const client_session& session,
  1041. bsoncxx::document::view_or_value document,
  1042. const options::insert& options = {});
  1043. ///
  1044. /// @}
  1045. ///
  1046. ///
  1047. /// @{
  1048. ///
  1049. /// Inserts multiple documents into the collection. If any of the documents are missing
  1050. /// identifiers the driver will generate them.
  1051. ///
  1052. /// @warning This method uses the bulk insert command to execute the insertion as opposed to
  1053. /// the legacy OP_INSERT wire protocol message. As a result, using this method to insert many
  1054. /// documents on MongoDB < 2.6 will be slow.
  1055. ///
  1056. /// @tparam container_type
  1057. /// The container type. Must meet the requirements for the container concept with a value
  1058. /// type of model::write.
  1059. ///
  1060. /// @param container
  1061. /// Container of a documents to insert.
  1062. /// @param options
  1063. /// Optional arguments, see options::insert.
  1064. ///
  1065. /// @return The optional result of attempting to performing the insert.
  1066. /// If the write concern is unacknowledged, the optional will be
  1067. /// disengaged.
  1068. ///
  1069. /// @throws mongocxx::bulk_write_exception when the operation fails.
  1070. ///
  1071. template <typename container_type>
  1072. MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
  1073. const container_type& container, const options::insert& options = options::insert());
  1074. ///
  1075. /// Inserts multiple documents into the collection. If any of the documents are missing
  1076. /// identifiers the driver will generate them.
  1077. ///
  1078. /// @tparam container_type
  1079. /// The container type. Must meet the requirements for the container concept with a value
  1080. /// type of model::write.
  1081. ///
  1082. /// @param session
  1083. /// The mongocxx::client_session with which to perform the inserts.
  1084. /// @param container
  1085. /// Container of a documents to insert.
  1086. /// @param options
  1087. /// Optional arguments, see options::insert.
  1088. ///
  1089. /// @return The optional result of attempting to performing the insert.
  1090. /// If the write concern is unacknowledged, the optional will be
  1091. /// disengaged.
  1092. ///
  1093. /// @throws mongocxx::bulk_write_exception when the operation fails.
  1094. ///
  1095. template <typename container_type>
  1096. MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
  1097. const client_session& session,
  1098. const container_type& container,
  1099. const options::insert& options = options::insert());
  1100. ///
  1101. /// @{
  1102. ///
  1103. /// Inserts multiple documents into the collection. If any of the documents are missing
  1104. /// identifiers the driver will generate them.
  1105. ///
  1106. /// @warning This method uses the bulk insert command to execute the insertion as opposed to
  1107. /// the legacy OP_INSERT wire protocol message. As a result, using this method to insert many
  1108. /// documents on MongoDB < 2.6 will be slow.
  1109. ///
  1110. /// @tparam document_view_iterator_type
  1111. /// The iterator type. Must meet the requirements for the input iterator concept with a value
  1112. /// type of bsoncxx::document::view.
  1113. ///
  1114. /// @param begin
  1115. /// Iterator pointing to the first document to be inserted.
  1116. /// @param end
  1117. /// Iterator pointing to the end of the documents to be inserted.
  1118. /// @param options
  1119. /// Optional arguments, see options::insert.
  1120. ///
  1121. /// @return The result of attempting to performing the insert.
  1122. ///
  1123. /// @throws mongocxx::bulk_write_exception if the operation fails.
  1124. ///
  1125. template <typename document_view_iterator_type>
  1126. MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
  1127. document_view_iterator_type begin,
  1128. document_view_iterator_type end,
  1129. const options::insert& options = options::insert());
  1130. ///
  1131. /// Inserts multiple documents into the collection. If any of the documents are missing
  1132. /// identifiers the driver will generate them.
  1133. ///
  1134. /// @tparam document_view_iterator_type
  1135. /// The iterator type. Must meet the requirements for the input iterator concept with a value
  1136. /// type of bsoncxx::document::view.
  1137. ///
  1138. /// @param session
  1139. /// The mongocxx::client_session with which to perform the inserts.
  1140. /// @param begin
  1141. /// Iterator pointing to the first document to be inserted.
  1142. /// @param end
  1143. /// Iterator pointing to the end of the documents to be inserted.
  1144. /// @param options
  1145. /// Optional arguments, see options::insert.
  1146. ///
  1147. /// @return The result of attempting to performing the insert.
  1148. ///
  1149. /// @throws mongocxx::bulk_write_exception if the operation fails.
  1150. ///
  1151. template <typename document_view_iterator_type>
  1152. MONGOCXX_INLINE stdx::optional<result::insert_many> insert_many(
  1153. const client_session& session,
  1154. document_view_iterator_type begin,
  1155. document_view_iterator_type end,
  1156. const options::insert& options = options::insert());
  1157. ///
  1158. /// @}
  1159. ///
  1160. ///
  1161. /// @{
  1162. ///
  1163. /// Returns a list of the indexes currently on this collection.
  1164. ///
  1165. /// @return Cursor yielding the index specifications.
  1166. ///
  1167. /// @throws mongocxx::operation_exception if the operation fails.
  1168. ///
  1169. /// @see https://docs.mongodb.com/master/reference/command/listIndexes/
  1170. ///
  1171. cursor list_indexes() const;
  1172. ///
  1173. /// Returns a list of the indexes currently on this collection.
  1174. ///
  1175. /// @param session
  1176. /// The mongocxx::client_session with which to perform the operation.
  1177. ///
  1178. /// @return Cursor yielding the index specifications.
  1179. ///
  1180. /// @throws mongocxx::operation_exception if the operation fails.
  1181. ///
  1182. /// @see https://docs.mongodb.com/master/reference/command/listIndexes/
  1183. ///
  1184. cursor list_indexes(const client_session& session) const;
  1185. ///
  1186. /// @}
  1187. ///
  1188. ///
  1189. /// Returns the name of this collection.
  1190. ///
  1191. /// @return The name of the collection. The return value of this method is invalidated by any
  1192. /// subsequent call to collection::rename() on this collection object.
  1193. ///
  1194. stdx::string_view name() const;
  1195. ///
  1196. /// Rename this collection.
  1197. ///
  1198. /// @param new_name The new name to assign to the collection.
  1199. /// @param drop_target_before_rename Whether to overwrite any
  1200. /// existing collections called new_name. The default is false.
  1201. /// @param write_concern (optional)
  1202. /// The write concern to use for this operation. Defaults to the collection wide write
  1203. /// concern if none is provided.
  1204. ///
  1205. /// @exception
  1206. /// mongocxx::operation_exception if the operation fails.
  1207. ///
  1208. /// @see
  1209. /// https://docs.mongodb.com/master/reference/command/renameCollection/
  1210. ///
  1211. /// @note
  1212. /// Write concern supported only for MongoDB 3.4+.
  1213. ///
  1214. void rename(bsoncxx::string::view_or_value new_name,
  1215. bool drop_target_before_rename = false,
  1216. const bsoncxx::stdx::optional<write_concern>& write_concern = {});
  1217. ///
  1218. /// Rename this collection.
  1219. ///
  1220. /// @param session
  1221. /// The mongocxx::client_session with which to perform the rename.
  1222. /// @param new_name The new name to assign to the collection.
  1223. /// @param drop_target_before_rename Whether to overwrite any
  1224. /// existing collections called new_name. The default is false.
  1225. /// @param write_concern (optional)
  1226. /// The write concern to use for this operation. Defaults to the collection wide write
  1227. /// concern if none is provided.
  1228. ///
  1229. /// @exception
  1230. /// mongocxx::operation_exception if the operation fails.
  1231. ///
  1232. /// @see
  1233. /// https://docs.mongodb.com/master/reference/command/renameCollection/
  1234. ///
  1235. /// @note
  1236. /// Write concern supported only for MongoDB 3.4+.
  1237. ///
  1238. void rename(const client_session& session,
  1239. bsoncxx::string::view_or_value new_name,
  1240. bool drop_target_before_rename = false,
  1241. const bsoncxx::stdx::optional<write_concern>& write_concern = {});
  1242. ///
  1243. /// @}
  1244. ///
  1245. ///
  1246. /// Sets the read_concern for this collection. Changes will not have any effect on existing
  1247. /// cursors or other read operations which use the previously-set read concern.
  1248. ///
  1249. /// @param rc
  1250. /// The new @c read_concern
  1251. ///
  1252. /// @see https://docs.mongodb.com/master/reference/read-concern/
  1253. ///
  1254. void read_concern(class read_concern rc);
  1255. ///
  1256. /// Gets the read_concern for the collection.
  1257. ///
  1258. /// If a read_concern has not been explicitly set for this collection object, it inherits
  1259. /// the read_concern from its parent database or client object.
  1260. ///
  1261. /// @return The current read_concern.
  1262. ///
  1263. class read_concern read_concern() const;
  1264. ///
  1265. /// Sets the read_preference for this collection. Changes will not have any effect on existing
  1266. /// cursors or other read operations which use the read preference.
  1267. ///
  1268. /// @param rp
  1269. /// The read_preference to set.
  1270. ///
  1271. /// @see https://docs.mongodb.com/master/core/read-preference/
  1272. ///
  1273. void read_preference(class read_preference rp);
  1274. ///
  1275. /// Gets the read_preference for the collection.
  1276. ///
  1277. /// @return The current read_preference.
  1278. ///
  1279. /// @see https://docs.mongodb.com/master/core/read-preference/
  1280. ///
  1281. class read_preference read_preference() const;
  1282. ///
  1283. /// @{
  1284. ///
  1285. /// Replaces a single document matching the provided filter in this collection.
  1286. ///
  1287. /// @param filter
  1288. /// Document representing the match criteria.
  1289. /// @param replacement
  1290. /// The replacement document.
  1291. /// @param options
  1292. /// Optional arguments, see options::replace.
  1293. ///
  1294. /// @return The optional result of attempting to replace a document.
  1295. /// If the write concern is unacknowledged, the optional will be
  1296. /// disengaged.
  1297. ///
  1298. /// @throws
  1299. /// mongocxx::logic_error if the replacement is invalid, or mongocxx::bulk_write_exception if
  1300. /// the operation fails.
  1301. ///
  1302. /// @see https://docs.mongodb.com/master/reference/command/update/
  1303. ///
  1304. stdx::optional<result::replace_one> replace_one(
  1305. bsoncxx::document::view_or_value filter,
  1306. bsoncxx::document::view_or_value replacement,
  1307. const options::replace& options = options::replace{});
  1308. ///
  1309. /// Replaces a single document matching the provided filter in this collection.
  1310. ///
  1311. /// @param session
  1312. /// The mongocxx::client_session with which to perform the replace.
  1313. /// @param filter
  1314. /// Document representing the match criteria.
  1315. /// @param replacement
  1316. /// The replacement document.
  1317. /// @param options
  1318. /// Optional arguments, see options::replace.
  1319. ///
  1320. /// @return The optional result of attempting to replace a document.
  1321. /// If the write concern is unacknowledged, the optional will be
  1322. /// disengaged.
  1323. ///
  1324. /// @throws
  1325. /// mongocxx::logic_error if the replacement is invalid, or mongocxx::bulk_write_exception if
  1326. /// the operation fails.
  1327. ///
  1328. /// @see https://docs.mongodb.com/master/reference/command/update/
  1329. ///
  1330. stdx::optional<result::replace_one> replace_one(
  1331. const client_session& session,
  1332. bsoncxx::document::view_or_value filter,
  1333. bsoncxx::document::view_or_value replacement,
  1334. const options::replace& options = options::replace{});
  1335. ///
  1336. /// @{
  1337. ///
  1338. /// Updates multiple documents matching the provided filter in this collection.
  1339. ///
  1340. /// @param filter
  1341. /// Document representing the match criteria.
  1342. /// @param update
  1343. /// Document representing the update to be applied to matching documents.
  1344. /// @param options
  1345. /// Optional arguments, see options::update.
  1346. ///
  1347. /// @return The optional result of attempting to update multiple documents.
  1348. /// If the write concern is unacknowledged, the optional will be
  1349. /// disengaged.
  1350. ///
  1351. /// @throws
  1352. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1353. /// operation fails.
  1354. ///
  1355. /// @see https://docs.mongodb.com/master/reference/command/update/
  1356. ///
  1357. stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
  1358. bsoncxx::document::view_or_value update,
  1359. const options::update& options = options::update());
  1360. ///
  1361. /// Updates multiple documents matching the provided filter in this collection.
  1362. ///
  1363. /// @param filter
  1364. /// Document representing the match criteria.
  1365. /// @param update
  1366. /// Pipeline representing the update to be applied to matching documents.
  1367. /// @param options
  1368. /// Optional arguments, see options::update.
  1369. ///
  1370. /// @return The optional result of attempting to update multiple documents.
  1371. /// If the write concern is unacknowledged, the optional will be
  1372. /// disengaged.
  1373. ///
  1374. /// @throws
  1375. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1376. /// operation fails.
  1377. ///
  1378. /// @see https://docs.mongodb.com/master/reference/command/update/
  1379. ///
  1380. stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
  1381. const pipeline& update,
  1382. const options::update& options = options::update());
  1383. ///
  1384. /// Updates multiple documents matching the provided filter in this collection.
  1385. ///
  1386. /// @param filter
  1387. /// Document representing the match criteria.
  1388. /// @param update
  1389. /// Supports the empty update {}.
  1390. /// @param options
  1391. /// Optional arguments, see options::update.
  1392. ///
  1393. /// @return The optional result of attempting to update multiple documents.
  1394. /// If the write concern is unacknowledged, the optional will be
  1395. /// disengaged.
  1396. ///
  1397. /// @throws
  1398. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1399. /// operation fails.
  1400. ///
  1401. /// @see https://docs.mongodb.com/master/reference/command/update/
  1402. ///
  1403. stdx::optional<result::update> update_many(bsoncxx::document::view_or_value filter,
  1404. std::initializer_list<_empty_doc_tag> update,
  1405. const options::update& options = options::update());
  1406. ///
  1407. /// Updates multiple documents matching the provided filter in this collection.
  1408. ///
  1409. /// @param session
  1410. /// The mongocxx::client_session with which to perform the update.
  1411. /// @param filter
  1412. /// Document representing the match criteria.
  1413. /// @param update
  1414. /// Document representing the update to be applied to matching documents.
  1415. /// @param options
  1416. /// Optional arguments, see options::update.
  1417. ///
  1418. /// @return The optional result of attempting to update multiple documents.
  1419. /// If the write concern is unacknowledged, the optional will be
  1420. /// disengaged.
  1421. ///
  1422. /// @throws
  1423. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1424. /// operation fails.
  1425. ///
  1426. /// @see https://docs.mongodb.com/master/reference/command/update/
  1427. ///
  1428. stdx::optional<result::update> update_many(const client_session& session,
  1429. bsoncxx::document::view_or_value filter,
  1430. bsoncxx::document::view_or_value update,
  1431. const options::update& options = options::update());
  1432. ///
  1433. /// Updates multiple documents matching the provided filter in this collection.
  1434. ///
  1435. /// @param session
  1436. /// The mongocxx::client_session with which to perform the update.
  1437. /// @param filter
  1438. /// Document representing the match criteria.
  1439. /// @param update
  1440. /// Pipeline representing the update to be applied to matching documents.
  1441. /// @param options
  1442. /// Optional arguments, see options::update.
  1443. ///
  1444. /// @return The optional result of attempting to update multiple documents.
  1445. /// If the write concern is unacknowledged, the optional will be
  1446. /// disengaged.
  1447. ///
  1448. /// @throws
  1449. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1450. /// operation fails.
  1451. ///
  1452. /// @see https://docs.mongodb.com/master/reference/command/update/
  1453. ///
  1454. stdx::optional<result::update> update_many(const client_session& session,
  1455. bsoncxx::document::view_or_value filter,
  1456. const pipeline& update,
  1457. const options::update& options = options::update());
  1458. ///
  1459. /// Updates multiple documents matching the provided filter in this collection.
  1460. ///
  1461. /// @param session
  1462. /// The mongocxx::client_session with which to perform the update.
  1463. /// @param filter
  1464. /// Document representing the match criteria.
  1465. /// @param update
  1466. /// Supports the empty update {}.
  1467. /// @param options
  1468. /// Optional arguments, see options::update.
  1469. ///
  1470. /// @return The optional result of attempting to update multiple documents.
  1471. /// If the write concern is unacknowledged, the optional will be
  1472. /// disengaged.
  1473. ///
  1474. /// @throws
  1475. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1476. /// operation fails.
  1477. ///
  1478. /// @see https://docs.mongodb.com/master/reference/command/update/
  1479. ///
  1480. stdx::optional<result::update> update_many(const client_session& session,
  1481. bsoncxx::document::view_or_value filter,
  1482. std::initializer_list<_empty_doc_tag> update,
  1483. const options::update& options = options::update());
  1484. ///
  1485. /// @}
  1486. ///
  1487. ///
  1488. /// @{
  1489. ///
  1490. /// Updates a single document matching the provided filter in this collection.
  1491. ///
  1492. /// @param filter
  1493. /// Document representing the match criteria.
  1494. /// @param update
  1495. /// Document representing the update to be applied to a matching document.
  1496. /// @param options
  1497. /// Optional arguments, see options::update.
  1498. ///
  1499. /// @return The optional result of attempting to update a document.
  1500. /// If the write concern is unacknowledged, the optional will be
  1501. /// disengaged.
  1502. ///
  1503. /// @throws
  1504. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1505. /// operation fails.
  1506. ///
  1507. /// @see https://docs.mongodb.com/master/reference/command/update/
  1508. ///
  1509. stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
  1510. bsoncxx::document::view_or_value update,
  1511. const options::update& options = options::update());
  1512. ///
  1513. /// Updates a single document matching the provided filter in this collection.
  1514. ///
  1515. /// @param filter
  1516. /// Document representing the match criteria.
  1517. /// @param update
  1518. /// Pipeline representing the update to be applied to a matching document.
  1519. /// @param options
  1520. /// Optional arguments, see options::update.
  1521. ///
  1522. /// @return The optional result of attempting to update a document.
  1523. /// If the write concern is unacknowledged, the optional will be
  1524. /// disengaged.
  1525. ///
  1526. /// @throws
  1527. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1528. /// operation fails.
  1529. ///
  1530. /// @see https://docs.mongodb.com/master/reference/command/update/
  1531. ///
  1532. stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
  1533. const pipeline& update,
  1534. const options::update& options = options::update());
  1535. ///
  1536. /// Updates a single document matching the provided filter in this collection.
  1537. ///
  1538. /// @param filter
  1539. /// Document representing the match criteria.
  1540. /// @param update
  1541. /// Supports the empty update {}.
  1542. /// @param options
  1543. /// Optional arguments, see options::update.
  1544. ///
  1545. /// @return The optional result of attempting to update a document.
  1546. /// If the write concern is unacknowledged, the optional will be
  1547. /// disengaged.
  1548. ///
  1549. /// @throws
  1550. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1551. /// operation fails.
  1552. ///
  1553. /// @see https://docs.mongodb.com/master/reference/command/update/
  1554. ///
  1555. stdx::optional<result::update> update_one(bsoncxx::document::view_or_value filter,
  1556. std::initializer_list<_empty_doc_tag> update,
  1557. const options::update& options = options::update());
  1558. ///
  1559. /// Updates a single document matching the provided filter in this collection.
  1560. ///
  1561. /// @param session
  1562. /// The mongocxx::client_session with which to perform the update.
  1563. /// @param filter
  1564. /// Document representing the match criteria.
  1565. /// @param update
  1566. /// Document representing the update to be applied to a matching document.
  1567. /// @param options
  1568. /// Optional arguments, see options::update.
  1569. ///
  1570. /// @return The optional result of attempting to update a document.
  1571. /// If the write concern is unacknowledged, the optional will be
  1572. /// disengaged.
  1573. ///
  1574. /// @throws
  1575. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1576. /// operation fails.
  1577. ///
  1578. /// @see https://docs.mongodb.com/master/reference/command/update/
  1579. ///
  1580. stdx::optional<result::update> update_one(const client_session& session,
  1581. bsoncxx::document::view_or_value filter,
  1582. bsoncxx::document::view_or_value update,
  1583. const options::update& options = options::update());
  1584. ///
  1585. /// Updates a single document matching the provided filter in this collection.
  1586. ///
  1587. /// @param session
  1588. /// The mongocxx::client_session with which to perform the update.
  1589. /// @param filter
  1590. /// Document representing the match criteria.
  1591. /// @param update
  1592. /// Pipeline representing the update to be applied to a matching document.
  1593. /// @param options
  1594. /// Optional arguments, see options::update.
  1595. ///
  1596. /// @return The optional result of attempting to update a document.
  1597. /// If the write concern is unacknowledged, the optional will be
  1598. /// disengaged.
  1599. ///
  1600. /// @throws
  1601. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1602. /// operation fails.
  1603. ///
  1604. /// @see https://docs.mongodb.com/master/reference/command/update/
  1605. ///
  1606. stdx::optional<result::update> update_one(const client_session& session,
  1607. bsoncxx::document::view_or_value filter,
  1608. const pipeline& update,
  1609. const options::update& options = options::update());
  1610. ///
  1611. /// Updates a single document matching the provided filter in this collection.
  1612. ///
  1613. /// @param session
  1614. /// The mongocxx::client_session with which to perform the update.
  1615. /// @param filter
  1616. /// Document representing the match criteria.
  1617. /// @param update
  1618. /// Supports the empty update {}.
  1619. /// @param options
  1620. /// Optional arguments, see options::update.
  1621. ///
  1622. /// @return The optional result of attempting to update a document.
  1623. /// If the write concern is unacknowledged, the optional will be
  1624. /// disengaged.
  1625. ///
  1626. /// @throws
  1627. /// mongocxx::logic_error if the update is invalid, or mongocxx::bulk_write_exception if the
  1628. /// operation fails.
  1629. ///
  1630. /// @see https://docs.mongodb.com/master/reference/command/update/
  1631. ///
  1632. stdx::optional<result::update> update_one(const client_session& session,
  1633. bsoncxx::document::view_or_value filter,
  1634. std::initializer_list<_empty_doc_tag> update,
  1635. const options::update& options = options::update());
  1636. ///
  1637. /// @}
  1638. ///
  1639. ///
  1640. /// Sets the write_concern for this collection. Changes will not have any effect on existing
  1641. /// write operations.
  1642. ///
  1643. /// @param wc
  1644. /// The new write_concern to use.
  1645. ///
  1646. void write_concern(class write_concern wc);
  1647. ///
  1648. /// Gets the write_concern for the collection.
  1649. ///
  1650. /// @return The current write_concern.
  1651. ///
  1652. class write_concern write_concern() const;
  1653. ///
  1654. /// Gets an index_view to the collection.
  1655. index_view indexes();
  1656. ///
  1657. /// @{
  1658. ///
  1659. /// Gets a change stream on this collection with an empty pipeline.
  1660. /// Change streams are only supported with a "majority" read concern or no read concern.
  1661. ///
  1662. /// @param options
  1663. /// The options to use when creating the change stream.
  1664. ///
  1665. /// @return
  1666. /// A change stream on this collection.
  1667. ///
  1668. /// @see https://docs.mongodb.com/manual/changeStreams/
  1669. ///
  1670. change_stream watch(const options::change_stream& options = {});
  1671. ///
  1672. /// @param session
  1673. /// The mongocxx::client_session with which to perform the watch operation.
  1674. /// @param options
  1675. /// The options to use when creating the change stream.
  1676. ///
  1677. /// @return
  1678. /// A change stream on this collection.
  1679. ///
  1680. /// @see https://docs.mongodb.com/manual/changeStreams/
  1681. ///
  1682. change_stream watch(const client_session& session, const options::change_stream& options = {});
  1683. ///
  1684. /// Gets a change stream on this collection.
  1685. /// Change streams are only supported with a "majority" read concern or no read concern.
  1686. ///
  1687. /// @param pipe
  1688. /// The aggregation pipeline to be used on the change notifications.
  1689. /// Only a subset of pipeline operations are supported for change streams. For more
  1690. /// information see the change streams documentation.
  1691. /// @param options
  1692. /// The options to use when creating the change stream.
  1693. ///
  1694. /// @return
  1695. /// A change stream on this collection.
  1696. ///
  1697. /// @see https://docs.mongodb.com/manual/changeStreams/
  1698. ///
  1699. change_stream watch(const pipeline& pipe, const options::change_stream& options = {});
  1700. ///
  1701. /// Gets a change stream on this collection.
  1702. ///
  1703. /// @param session
  1704. /// The mongocxx::client_session with which to perform the watch operation.
  1705. /// @param pipe
  1706. /// The aggregation pipeline to be used on the change notifications.
  1707. /// @param options
  1708. /// The options to use when creating the change stream.
  1709. ///
  1710. /// @return
  1711. /// A change stream on this collection.
  1712. ///
  1713. /// @see https://docs.mongodb.com/manual/changeStreams/
  1714. ///
  1715. change_stream watch(const client_session& session,
  1716. const pipeline& pipe,
  1717. const options::change_stream& options = {});
  1718. ///
  1719. /// @}
  1720. ///
  1721. private:
  1722. friend class bulk_write;
  1723. friend class database;
  1724. MONGOCXX_PRIVATE collection(const database& database,
  1725. bsoncxx::string::view_or_value collection_name);
  1726. MONGOCXX_PRIVATE collection(const database& database, void* collection);
  1727. MONGOCXX_PRIVATE cursor _aggregate(const client_session* session,
  1728. const pipeline& pipeline,
  1729. const options::aggregate& options);
  1730. MONGOCXX_PRIVATE std::int64_t _count(const client_session* session,
  1731. bsoncxx::document::view_or_value filter,
  1732. const options::count& options);
  1733. MONGOCXX_PRIVATE std::int64_t _count_documents(const client_session* session,
  1734. bsoncxx::document::view_or_value filter,
  1735. const options::count& options);
  1736. MONGOCXX_PRIVATE bsoncxx::document::value _create_index(
  1737. const client_session* session,
  1738. bsoncxx::document::view_or_value keys,
  1739. bsoncxx::document::view_or_value index_options,
  1740. options::index_view operation_options);
  1741. MONGOCXX_PRIVATE stdx::optional<result::delete_result> _delete_many(
  1742. const client_session* session,
  1743. bsoncxx::document::view_or_value filter,
  1744. const options::delete_options& options);
  1745. MONGOCXX_PRIVATE stdx::optional<result::delete_result> _delete_one(
  1746. const client_session* session,
  1747. bsoncxx::document::view_or_value filter,
  1748. const options::delete_options& options);
  1749. MONGOCXX_PRIVATE cursor _distinct(const client_session* session,
  1750. bsoncxx::string::view_or_value name,
  1751. bsoncxx::document::view_or_value filter,
  1752. const options::distinct& options);
  1753. MONGOCXX_PRIVATE void _drop(
  1754. const client_session* session,
  1755. const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern);
  1756. MONGOCXX_PRIVATE cursor _find(const client_session* session,
  1757. bsoncxx::document::view_or_value filter,
  1758. const options::find& options);
  1759. MONGOCXX_PRIVATE stdx::optional<bsoncxx::document::value> _find_one(
  1760. const client_session* session,
  1761. bsoncxx::document::view_or_value filter,
  1762. const options::find& options);
  1763. MONGOCXX_PRIVATE stdx::optional<bsoncxx::document::value> _find_one_and_delete(
  1764. const client_session* session,
  1765. bsoncxx::document::view_or_value filter,
  1766. const options::find_one_and_delete& options);
  1767. MONGOCXX_PRIVATE stdx::optional<bsoncxx::document::value> _find_one_and_replace(
  1768. const client_session* session,
  1769. bsoncxx::document::view_or_value filter,
  1770. bsoncxx::document::view_or_value replacement,
  1771. const options::find_one_and_replace& options);
  1772. MONGOCXX_PRIVATE stdx::optional<bsoncxx::document::value> _find_one_and_update(
  1773. const client_session* session,
  1774. bsoncxx::document::view_or_value filter,
  1775. bsoncxx::document::view_or_value update,
  1776. const options::find_one_and_update& options);
  1777. MONGOCXX_PRIVATE stdx::optional<result::insert_one> _insert_one(
  1778. const client_session* session,
  1779. bsoncxx::document::view_or_value document,
  1780. const options::insert& options);
  1781. MONGOCXX_PRIVATE void _rename(
  1782. const client_session* session,
  1783. bsoncxx::string::view_or_value new_name,
  1784. bool drop_target_before_rename,
  1785. const bsoncxx::stdx::optional<class write_concern>& write_concern);
  1786. MONGOCXX_PRIVATE stdx::optional<result::replace_one> _replace_one(
  1787. const client_session* session,
  1788. const options::bulk_write& bulk_opts,
  1789. const model::replace_one& replace_op);
  1790. MONGOCXX_PRIVATE stdx::optional<result::replace_one> _replace_one(
  1791. const client_session* session,
  1792. bsoncxx::document::view_or_value filter,
  1793. bsoncxx::document::view_or_value replacement,
  1794. const options::replace& options);
  1795. MONGOCXX_PRIVATE stdx::optional<result::update> _update_one(
  1796. const client_session* session,
  1797. bsoncxx::document::view_or_value filter,
  1798. bsoncxx::document::view_or_value update,
  1799. const options::update& options);
  1800. MONGOCXX_PRIVATE stdx::optional<result::update> _update_many(
  1801. const client_session* session,
  1802. bsoncxx::document::view_or_value filter,
  1803. bsoncxx::document::view_or_value update,
  1804. const options::update& options);
  1805. MONGOCXX_PRIVATE change_stream _watch(const client_session* session,
  1806. const pipeline& pipe,
  1807. const options::change_stream& options);
  1808. // Helpers for the insert_many method templates.
  1809. class bulk_write _init_insert_many(const options::insert& options,
  1810. const client_session* session);
  1811. void _insert_many_doc_handler(class bulk_write& writes,
  1812. bsoncxx::builder::basic::array& inserted_ids,
  1813. bsoncxx::document::view doc) const;
  1814. stdx::optional<result::insert_many> _exec_insert_many(
  1815. class bulk_write& writes, bsoncxx::builder::basic::array& inserted_ids);
  1816. template <typename document_view_iterator_type>
  1817. MONGOCXX_PRIVATE stdx::optional<result::insert_many> _insert_many(
  1818. const client_session* session,
  1819. document_view_iterator_type begin,
  1820. document_view_iterator_type end,
  1821. const options::insert& options);
  1822. class MONGOCXX_PRIVATE impl;
  1823. MONGOCXX_PRIVATE impl& _get_impl();
  1824. MONGOCXX_PRIVATE const impl& _get_impl() const;
  1825. std::unique_ptr<impl> _impl;
  1826. };
  1827. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::write(
  1828. const model::write& write, const options::bulk_write& options) {
  1829. return create_bulk_write(options).append(write).execute();
  1830. }
  1831. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::write(
  1832. const client_session& session, const model::write& write, const options::bulk_write& options) {
  1833. return create_bulk_write(session, options).append(write).execute();
  1834. }
  1835. template <typename container_type>
  1836. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
  1837. const container_type& requests, const options::bulk_write& options) {
  1838. return bulk_write(requests.begin(), requests.end(), options);
  1839. }
  1840. template <typename container_type>
  1841. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
  1842. const client_session& session,
  1843. const container_type& requests,
  1844. const options::bulk_write& options) {
  1845. return bulk_write(session, requests.begin(), requests.end(), options);
  1846. }
  1847. template <typename write_model_iterator_type>
  1848. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
  1849. write_model_iterator_type begin,
  1850. write_model_iterator_type end,
  1851. const options::bulk_write& options) {
  1852. auto writes = create_bulk_write(options);
  1853. std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
  1854. return writes.execute();
  1855. }
  1856. template <typename write_model_iterator_type>
  1857. MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
  1858. const client_session& session,
  1859. write_model_iterator_type begin,
  1860. write_model_iterator_type end,
  1861. const options::bulk_write& options) {
  1862. auto writes = create_bulk_write(session, options);
  1863. std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
  1864. return writes.execute();
  1865. }
  1866. template <typename container_type>
  1867. MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
  1868. const container_type& container, const options::insert& options) {
  1869. return insert_many(container.begin(), container.end(), options);
  1870. }
  1871. template <typename container_type>
  1872. MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
  1873. const client_session& session,
  1874. const container_type& container,
  1875. const options::insert& options) {
  1876. return insert_many(session, container.begin(), container.end(), options);
  1877. }
  1878. template <typename document_view_iterator_type>
  1879. MONGOCXX_INLINE stdx::optional<result::insert_many> collection::_insert_many(
  1880. const client_session* session,
  1881. document_view_iterator_type begin,
  1882. document_view_iterator_type end,
  1883. const options::insert& options) {
  1884. bsoncxx::builder::basic::array inserted_ids;
  1885. auto writes = _init_insert_many(options, session);
  1886. std::for_each(begin, end, [&inserted_ids, &writes, this](bsoncxx::document::view doc) {
  1887. _insert_many_doc_handler(writes, inserted_ids, doc);
  1888. });
  1889. return _exec_insert_many(writes, inserted_ids);
  1890. }
  1891. template <typename document_view_iterator_type>
  1892. MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
  1893. document_view_iterator_type begin,
  1894. document_view_iterator_type end,
  1895. const options::insert& options) {
  1896. return _insert_many(nullptr, begin, end, options);
  1897. }
  1898. template <typename document_view_iterator_type>
  1899. MONGOCXX_INLINE stdx::optional<result::insert_many> collection::insert_many(
  1900. const client_session& session,
  1901. document_view_iterator_type begin,
  1902. document_view_iterator_type end,
  1903. const options::insert& options) {
  1904. return _insert_many(&session, begin, end, options);
  1905. }
  1906. MONGOCXX_INLINE_NAMESPACE_END
  1907. } // namespace mongocxx
  1908. #include <mongocxx/config/postlude.hpp>