IMongoCollectionExtensions.cs 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  1. /* Copyright 2010-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. */
  15. using System;
  16. using System.Linq;
  17. using System.Linq.Expressions;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using MongoDB.Driver.Core.Misc;
  21. using MongoDB.Driver.Linq;
  22. namespace MongoDB.Driver
  23. {
  24. /// <summary>
  25. /// Extension methods for <see cref="IMongoCollection{T}"/>.
  26. /// </summary>
  27. public static class IMongoCollectionExtensions
  28. {
  29. /// <summary>
  30. /// Begins a fluent aggregation interface.
  31. /// </summary>
  32. /// <typeparam name="TDocument">The type of the document.</typeparam>
  33. /// <param name="collection">The collection.</param>
  34. /// <param name="options">The options.</param>
  35. /// <returns>
  36. /// A fluent aggregate interface.
  37. /// </returns>
  38. public static IAggregateFluent<TDocument> Aggregate<TDocument>(this IMongoCollection<TDocument> collection, AggregateOptions options = null)
  39. {
  40. var emptyPipeline = new EmptyPipelineDefinition<TDocument>(collection.DocumentSerializer);
  41. return new AggregateFluent<TDocument, TDocument>(null, collection, emptyPipeline, options ?? new AggregateOptions());
  42. }
  43. /// <summary>
  44. /// Begins a fluent aggregation interface.
  45. /// </summary>
  46. /// <typeparam name="TDocument">The type of the document.</typeparam>
  47. /// <param name="collection">The collection.</param>
  48. /// <param name="session">The session.</param>
  49. /// <param name="options">The options.</param>
  50. /// <returns>
  51. /// A fluent aggregate interface.
  52. /// </returns>
  53. public static IAggregateFluent<TDocument> Aggregate<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, AggregateOptions options = null)
  54. {
  55. Ensure.IsNotNull(session, nameof(session));
  56. var emptyPipeline = new EmptyPipelineDefinition<TDocument>(collection.DocumentSerializer);
  57. return new AggregateFluent<TDocument, TDocument>(session, collection, emptyPipeline, options ?? new AggregateOptions());
  58. }
  59. /// <summary>
  60. /// Creates a queryable source of documents.
  61. /// </summary>
  62. /// <typeparam name="TDocument">The type of the document.</typeparam>
  63. /// <param name="collection">The collection.</param>
  64. /// <param name="aggregateOptions">The aggregate options</param>
  65. /// <returns>A queryable source of documents.</returns>
  66. public static IMongoQueryable<TDocument> AsQueryable<TDocument>(this IMongoCollection<TDocument> collection, AggregateOptions aggregateOptions = null)
  67. {
  68. Ensure.IsNotNull(collection, nameof(collection));
  69. aggregateOptions = aggregateOptions ?? new AggregateOptions();
  70. var provider = new MongoQueryProviderImpl<TDocument>(collection, aggregateOptions);
  71. return new MongoQueryableImpl<TDocument, TDocument>(provider);
  72. }
  73. /// <summary>
  74. /// Counts the number of documents in the collection.
  75. /// </summary>
  76. /// <typeparam name="TDocument">The type of the document.</typeparam>
  77. /// <param name="collection">The collection.</param>
  78. /// <param name="filter">The filter.</param>
  79. /// <param name="options">The options.</param>
  80. /// <param name="cancellationToken">The cancellation token.</param>
  81. /// <returns>
  82. /// The number of documents in the collection.
  83. /// </returns>
  84. [Obsolete("Use CountDocuments or EstimatedDocumentCount instead.")]
  85. public static long Count<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  86. {
  87. Ensure.IsNotNull(collection, nameof(collection));
  88. Ensure.IsNotNull(filter, nameof(filter));
  89. return collection.Count(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  90. }
  91. /// <summary>
  92. /// Counts the number of documents in the collection.
  93. /// </summary>
  94. /// <typeparam name="TDocument">The type of the document.</typeparam>
  95. /// <param name="session">The session.</param>
  96. /// <param name="collection">The collection.</param>
  97. /// <param name="filter">The filter.</param>
  98. /// <param name="options">The options.</param>
  99. /// <param name="cancellationToken">The cancellation token.</param>
  100. /// <returns>
  101. /// The number of documents in the collection.
  102. /// </returns>
  103. [Obsolete("Use CountDocuments or EstimatedDocumentCount instead.")]
  104. public static long Count<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  105. {
  106. Ensure.IsNotNull(collection, nameof(collection));
  107. Ensure.IsNotNull(session, nameof(session));
  108. Ensure.IsNotNull(filter, nameof(filter));
  109. return collection.Count(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  110. }
  111. /// <summary>
  112. /// Counts the number of documents in the collection.
  113. /// </summary>
  114. /// <typeparam name="TDocument">The type of the document.</typeparam>
  115. /// <param name="collection">The collection.</param>
  116. /// <param name="filter">The filter.</param>
  117. /// <param name="options">The options.</param>
  118. /// <param name="cancellationToken">The cancellation token.</param>
  119. /// <returns>
  120. /// The number of documents in the collection.
  121. /// </returns>
  122. [Obsolete("Use CountDocumentsAsync or EstimatedDocumentCountAsync instead.")]
  123. public static Task<long> CountAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  124. {
  125. Ensure.IsNotNull(collection, nameof(collection));
  126. Ensure.IsNotNull(filter, nameof(filter));
  127. return collection.CountAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  128. }
  129. /// <summary>
  130. /// Counts the number of documents in the collection.
  131. /// </summary>
  132. /// <typeparam name="TDocument">The type of the document.</typeparam>
  133. /// <param name="collection">The collection.</param>
  134. /// <param name="session">The session.</param>
  135. /// <param name="filter">The filter.</param>
  136. /// <param name="options">The options.</param>
  137. /// <param name="cancellationToken">The cancellation token.</param>
  138. /// <returns>
  139. /// The number of documents in the collection.
  140. /// </returns>
  141. [Obsolete("Use CountDocumentsAsync or EstimatedDocumentCountAsync instead.")]
  142. public static Task<long> CountAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  143. {
  144. Ensure.IsNotNull(collection, nameof(collection));
  145. Ensure.IsNotNull(session, nameof(session));
  146. Ensure.IsNotNull(filter, nameof(filter));
  147. return collection.CountAsync(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  148. }
  149. /// <summary>
  150. /// Counts the number of documents in the collection.
  151. /// </summary>
  152. /// <typeparam name="TDocument">The type of the document.</typeparam>
  153. /// <param name="collection">The collection.</param>
  154. /// <param name="filter">The filter.</param>
  155. /// <param name="options">The options.</param>
  156. /// <param name="cancellationToken">The cancellation token.</param>
  157. /// <returns>
  158. /// The number of documents in the collection.
  159. /// </returns>
  160. public static long CountDocuments<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  161. {
  162. Ensure.IsNotNull(collection, nameof(collection));
  163. Ensure.IsNotNull(filter, nameof(filter));
  164. return collection.CountDocuments(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  165. }
  166. /// <summary>
  167. /// Counts the number of documents in the collection.
  168. /// </summary>
  169. /// <typeparam name="TDocument">The type of the document.</typeparam>
  170. /// <param name="session">The session.</param>
  171. /// <param name="collection">The collection.</param>
  172. /// <param name="filter">The filter.</param>
  173. /// <param name="options">The options.</param>
  174. /// <param name="cancellationToken">The cancellation token.</param>
  175. /// <returns>
  176. /// The number of documents in the collection.
  177. /// </returns>
  178. public static long CountDocuments<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  179. {
  180. Ensure.IsNotNull(collection, nameof(collection));
  181. Ensure.IsNotNull(session, nameof(session));
  182. Ensure.IsNotNull(filter, nameof(filter));
  183. return collection.CountDocuments(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  184. }
  185. /// <summary>
  186. /// Counts the number of documents in the collection.
  187. /// </summary>
  188. /// <typeparam name="TDocument">The type of the document.</typeparam>
  189. /// <param name="collection">The collection.</param>
  190. /// <param name="filter">The filter.</param>
  191. /// <param name="options">The options.</param>
  192. /// <param name="cancellationToken">The cancellation token.</param>
  193. /// <returns>
  194. /// The number of documents in the collection.
  195. /// </returns>
  196. public static Task<long> CountDocumentsAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  197. {
  198. Ensure.IsNotNull(collection, nameof(collection));
  199. Ensure.IsNotNull(filter, nameof(filter));
  200. return collection.CountDocumentsAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  201. }
  202. /// <summary>
  203. /// Counts the number of documents in the collection.
  204. /// </summary>
  205. /// <typeparam name="TDocument">The type of the document.</typeparam>
  206. /// <param name="collection">The collection.</param>
  207. /// <param name="session">The session.</param>
  208. /// <param name="filter">The filter.</param>
  209. /// <param name="options">The options.</param>
  210. /// <param name="cancellationToken">The cancellation token.</param>
  211. /// <returns>
  212. /// The number of documents in the collection.
  213. /// </returns>
  214. public static Task<long> CountDocumentsAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  215. {
  216. Ensure.IsNotNull(collection, nameof(collection));
  217. Ensure.IsNotNull(session, nameof(session));
  218. Ensure.IsNotNull(filter, nameof(filter));
  219. return collection.CountDocumentsAsync(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  220. }
  221. /// <summary>
  222. /// Deletes multiple documents.
  223. /// </summary>
  224. /// <typeparam name="TDocument">The type of the document.</typeparam>
  225. /// <param name="collection">The collection.</param>
  226. /// <param name="filter">The filter.</param>
  227. /// <param name="cancellationToken">The cancellation token.</param>
  228. /// <returns>
  229. /// The result of the delete operation.
  230. /// </returns>
  231. public static DeleteResult DeleteMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  232. {
  233. return collection.DeleteMany(new ExpressionFilterDefinition<TDocument>(filter), cancellationToken);
  234. }
  235. /// <summary>
  236. /// Deletes multiple documents.
  237. /// </summary>
  238. /// <typeparam name="TDocument">The type of the document.</typeparam>
  239. /// <param name="collection">The collection.</param>
  240. /// <param name="filter">The filter.</param>
  241. /// <param name="options">The options.</param>
  242. /// <param name="cancellationToken">The cancellation token.</param>
  243. /// <returns>
  244. /// The result of the delete operation.
  245. /// </returns>
  246. public static DeleteResult DeleteMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  247. {
  248. Ensure.IsNotNull(collection, nameof(collection));
  249. Ensure.IsNotNull(filter, nameof(filter));
  250. return collection.DeleteMany(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  251. }
  252. /// <summary>
  253. /// Deletes multiple documents.
  254. /// </summary>
  255. /// <typeparam name="TDocument">The type of the document.</typeparam>
  256. /// <param name="collection">The collection.</param>
  257. /// <param name="session">The session.</param>
  258. /// <param name="filter">The filter.</param>
  259. /// <param name="options">The options.</param>
  260. /// <param name="cancellationToken">The cancellation token.</param>
  261. /// <returns>
  262. /// The result of the delete operation.
  263. /// </returns>
  264. public static DeleteResult DeleteMany<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, DeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  265. {
  266. Ensure.IsNotNull(collection, nameof(collection));
  267. Ensure.IsNotNull(session, nameof(session));
  268. Ensure.IsNotNull(filter, nameof(filter));
  269. return collection.DeleteMany(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  270. }
  271. /// <summary>
  272. /// Deletes multiple documents.
  273. /// </summary>
  274. /// <typeparam name="TDocument">The type of the document.</typeparam>
  275. /// <param name="collection">The collection.</param>
  276. /// <param name="filter">The filter.</param>
  277. /// <param name="cancellationToken">The cancellation token.</param>
  278. /// <returns>
  279. /// The result of the delete operation.
  280. /// </returns>
  281. public static Task<DeleteResult> DeleteManyAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  282. {
  283. return collection.DeleteManyAsync(new ExpressionFilterDefinition<TDocument>(filter), cancellationToken);
  284. }
  285. /// <summary>
  286. /// Deletes multiple documents.
  287. /// </summary>
  288. /// <typeparam name="TDocument">The type of the document.</typeparam>
  289. /// <param name="collection">The collection.</param>
  290. /// <param name="filter">The filter.</param>
  291. /// <param name="options">The options.</param>
  292. /// <param name="cancellationToken">The cancellation token.</param>
  293. /// <returns>
  294. /// The result of the delete operation.
  295. /// </returns>
  296. public static Task<DeleteResult> DeleteManyAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  297. {
  298. Ensure.IsNotNull(collection, nameof(collection));
  299. Ensure.IsNotNull(filter, nameof(filter));
  300. return collection.DeleteManyAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  301. }
  302. /// <summary>
  303. /// Deletes multiple documents.
  304. /// </summary>
  305. /// <typeparam name="TDocument">The type of the document.</typeparam>
  306. /// <param name="collection">The collection.</param>
  307. /// <param name="session">The session.</param>
  308. /// <param name="filter">The filter.</param>
  309. /// <param name="options">The options.</param>
  310. /// <param name="cancellationToken">The cancellation token.</param>
  311. /// <returns>
  312. /// The result of the delete operation.
  313. /// </returns>
  314. public static Task<DeleteResult> DeleteManyAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, DeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  315. {
  316. Ensure.IsNotNull(collection, nameof(collection));
  317. Ensure.IsNotNull(session, nameof(session));
  318. Ensure.IsNotNull(filter, nameof(filter));
  319. return collection.DeleteManyAsync(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  320. }
  321. /// <summary>
  322. /// Deletes a single document.
  323. /// </summary>
  324. /// <typeparam name="TDocument">The type of the document.</typeparam>
  325. /// <param name="collection">The collection.</param>
  326. /// <param name="filter">The filter.</param>
  327. /// <param name="cancellationToken">The cancellation token.</param>
  328. /// <returns>
  329. /// The result of the delete operation.
  330. /// </returns>
  331. public static DeleteResult DeleteOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  332. {
  333. return collection.DeleteOne(new ExpressionFilterDefinition<TDocument>(filter), cancellationToken);
  334. }
  335. /// <summary>
  336. /// Deletes a single document.
  337. /// </summary>
  338. /// <typeparam name="TDocument">The type of the document.</typeparam>
  339. /// <param name="collection">The collection.</param>
  340. /// <param name="filter">The filter.</param>
  341. /// <param name="options">The options.</param>
  342. /// <param name="cancellationToken">The cancellation token.</param>
  343. /// <returns>
  344. /// The result of the delete operation.
  345. /// </returns>
  346. public static DeleteResult DeleteOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  347. {
  348. Ensure.IsNotNull(collection, nameof(collection));
  349. Ensure.IsNotNull(filter, nameof(filter));
  350. return collection.DeleteOne(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  351. }
  352. /// <summary>
  353. /// Deletes a single document.
  354. /// </summary>
  355. /// <typeparam name="TDocument">The type of the document.</typeparam>
  356. /// <param name="collection">The collection.</param>
  357. /// <param name="session">The session.</param>
  358. /// <param name="filter">The filter.</param>
  359. /// <param name="options">The options.</param>
  360. /// <param name="cancellationToken">The cancellation token.</param>
  361. /// <returns>
  362. /// The result of the delete operation.
  363. /// </returns>
  364. public static DeleteResult DeleteOne<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, DeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  365. {
  366. Ensure.IsNotNull(collection, nameof(collection));
  367. Ensure.IsNotNull(session, nameof(session));
  368. Ensure.IsNotNull(filter, nameof(filter));
  369. return collection.DeleteOne(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  370. }
  371. /// <summary>
  372. /// Deletes a single document.
  373. /// </summary>
  374. /// <typeparam name="TDocument">The type of the document.</typeparam>
  375. /// <param name="collection">The collection.</param>
  376. /// <param name="filter">The filter.</param>
  377. /// <param name="cancellationToken">The cancellation token.</param>
  378. /// <returns>
  379. /// The result of the delete operation.
  380. /// </returns>
  381. public static Task<DeleteResult> DeleteOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  382. {
  383. return collection.DeleteOneAsync(new ExpressionFilterDefinition<TDocument>(filter), cancellationToken);
  384. }
  385. /// <summary>
  386. /// Deletes a single document.
  387. /// </summary>
  388. /// <typeparam name="TDocument">The type of the document.</typeparam>
  389. /// <param name="collection">The collection.</param>
  390. /// <param name="filter">The filter.</param>
  391. /// <param name="options">The options.</param>
  392. /// <param name="cancellationToken">The cancellation token.</param>
  393. /// <returns>
  394. /// The result of the delete operation.
  395. /// </returns>
  396. public static Task<DeleteResult> DeleteOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  397. {
  398. Ensure.IsNotNull(collection, nameof(collection));
  399. Ensure.IsNotNull(filter, nameof(filter));
  400. return collection.DeleteOneAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  401. }
  402. /// <summary>
  403. /// Deletes a single document.
  404. /// </summary>
  405. /// <typeparam name="TDocument">The type of the document.</typeparam>
  406. /// <param name="collection">The collection.</param>
  407. /// <param name="session">The session.</param>
  408. /// <param name="filter">The filter.</param>
  409. /// <param name="options">The options.</param>
  410. /// <param name="cancellationToken">The cancellation token.</param>
  411. /// <returns>
  412. /// The result of the delete operation.
  413. /// </returns>
  414. public static Task<DeleteResult> DeleteOneAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, DeleteOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  415. {
  416. Ensure.IsNotNull(collection, nameof(collection));
  417. Ensure.IsNotNull(session, nameof(session));
  418. Ensure.IsNotNull(filter, nameof(filter));
  419. return collection.DeleteOneAsync(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  420. }
  421. /// <summary>
  422. /// Gets the distinct values for a specified field.
  423. /// </summary>
  424. /// <typeparam name="TDocument">The type of the document.</typeparam>
  425. /// <typeparam name="TField">The type of the result.</typeparam>
  426. /// <param name="collection">The collection.</param>
  427. /// <param name="field">The field.</param>
  428. /// <param name="filter">The filter.</param>
  429. /// <param name="options">The options.</param>
  430. /// <param name="cancellationToken">The cancellation token.</param>
  431. /// <returns>
  432. /// The distinct values for the specified field.
  433. /// </returns>
  434. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, TField>> field, FilterDefinition<TDocument> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  435. {
  436. Ensure.IsNotNull(collection, nameof(collection));
  437. Ensure.IsNotNull(field, nameof(field));
  438. Ensure.IsNotNull(filter, nameof(filter));
  439. return collection.Distinct(
  440. new ExpressionFieldDefinition<TDocument, TField>(field),
  441. filter,
  442. options,
  443. cancellationToken);
  444. }
  445. /// <summary>
  446. /// Gets the distinct values for a specified field.
  447. /// </summary>
  448. /// <typeparam name="TDocument">The type of the document.</typeparam>
  449. /// <typeparam name="TField">The type of the result.</typeparam>
  450. /// <param name="collection">The collection.</param>
  451. /// <param name="field">The field.</param>
  452. /// <param name="filter">The filter.</param>
  453. /// <param name="options">The options.</param>
  454. /// <param name="cancellationToken">The cancellation token.</param>
  455. /// <returns>
  456. /// The distinct values for the specified field.
  457. /// </returns>
  458. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, FieldDefinition<TDocument, TField> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  459. {
  460. Ensure.IsNotNull(collection, nameof(collection));
  461. Ensure.IsNotNull(field, nameof(field));
  462. Ensure.IsNotNull(filter, nameof(filter));
  463. return collection.Distinct(
  464. field,
  465. new ExpressionFilterDefinition<TDocument>(filter),
  466. options,
  467. cancellationToken);
  468. }
  469. /// <summary>
  470. /// Gets the distinct values for a specified field.
  471. /// </summary>
  472. /// <typeparam name="TDocument">The type of the document.</typeparam>
  473. /// <typeparam name="TField">The type of the result.</typeparam>
  474. /// <param name="collection">The collection.</param>
  475. /// <param name="field">The field.</param>
  476. /// <param name="filter">The filter.</param>
  477. /// <param name="options">The options.</param>
  478. /// <param name="cancellationToken">The cancellation token.</param>
  479. /// <returns>
  480. /// The distinct values for the specified field.
  481. /// </returns>
  482. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, TField>> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  483. {
  484. Ensure.IsNotNull(collection, nameof(collection));
  485. Ensure.IsNotNull(field, nameof(field));
  486. Ensure.IsNotNull(filter, nameof(filter));
  487. return collection.Distinct(
  488. new ExpressionFieldDefinition<TDocument, TField>(field),
  489. new ExpressionFilterDefinition<TDocument>(filter),
  490. options,
  491. cancellationToken);
  492. }
  493. /// <summary>
  494. /// Gets the distinct values for a specified field.
  495. /// </summary>
  496. /// <typeparam name="TDocument">The type of the document.</typeparam>
  497. /// <typeparam name="TField">The type of the result.</typeparam>
  498. /// <param name="collection">The collection.</param>
  499. /// <param name="session">The session.</param>
  500. /// <param name="field">The field.</param>
  501. /// <param name="filter">The filter.</param>
  502. /// <param name="options">The options.</param>
  503. /// <param name="cancellationToken">The cancellation token.</param>
  504. /// <returns>
  505. /// The distinct values for the specified field.
  506. /// </returns>
  507. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, TField>> field, FilterDefinition<TDocument> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  508. {
  509. Ensure.IsNotNull(collection, nameof(collection));
  510. Ensure.IsNotNull(session, nameof(session));
  511. Ensure.IsNotNull(field, nameof(field));
  512. Ensure.IsNotNull(filter, nameof(filter));
  513. return collection.Distinct(
  514. session,
  515. new ExpressionFieldDefinition<TDocument, TField>(field),
  516. filter,
  517. options,
  518. cancellationToken);
  519. }
  520. /// <summary>
  521. /// Gets the distinct values for a specified field.
  522. /// </summary>
  523. /// <typeparam name="TDocument">The type of the document.</typeparam>
  524. /// <typeparam name="TField">The type of the result.</typeparam>
  525. /// <param name="collection">The collection.</param>
  526. /// <param name="session">The session.</param>
  527. /// <param name="field">The field.</param>
  528. /// <param name="filter">The filter.</param>
  529. /// <param name="options">The options.</param>
  530. /// <param name="cancellationToken">The cancellation token.</param>
  531. /// <returns>
  532. /// The distinct values for the specified field.
  533. /// </returns>
  534. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FieldDefinition<TDocument, TField> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  535. {
  536. Ensure.IsNotNull(collection, nameof(collection));
  537. Ensure.IsNotNull(session, nameof(session));
  538. Ensure.IsNotNull(field, nameof(field));
  539. Ensure.IsNotNull(filter, nameof(filter));
  540. return collection.Distinct(
  541. session,
  542. field,
  543. new ExpressionFilterDefinition<TDocument>(filter),
  544. options,
  545. cancellationToken);
  546. }
  547. /// <summary>
  548. /// Gets the distinct values for a specified field.
  549. /// </summary>
  550. /// <typeparam name="TDocument">The type of the document.</typeparam>
  551. /// <typeparam name="TField">The type of the result.</typeparam>
  552. /// <param name="collection">The collection.</param>
  553. /// <param name="session">The session.</param>
  554. /// <param name="field">The field.</param>
  555. /// <param name="filter">The filter.</param>
  556. /// <param name="options">The options.</param>
  557. /// <param name="cancellationToken">The cancellation token.</param>
  558. /// <returns>
  559. /// The distinct values for the specified field.
  560. /// </returns>
  561. public static IAsyncCursor<TField> Distinct<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, TField>> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  562. {
  563. Ensure.IsNotNull(collection, nameof(collection));
  564. Ensure.IsNotNull(session, nameof(session));
  565. Ensure.IsNotNull(field, nameof(field));
  566. Ensure.IsNotNull(filter, nameof(filter));
  567. return collection.Distinct(
  568. session,
  569. new ExpressionFieldDefinition<TDocument, TField>(field),
  570. new ExpressionFilterDefinition<TDocument>(filter),
  571. options,
  572. cancellationToken);
  573. }
  574. /// <summary>
  575. /// Gets the distinct values for a specified field.
  576. /// </summary>
  577. /// <typeparam name="TDocument">The type of the document.</typeparam>
  578. /// <typeparam name="TField">The type of the result.</typeparam>
  579. /// <param name="collection">The collection.</param>
  580. /// <param name="field">The field.</param>
  581. /// <param name="filter">The filter.</param>
  582. /// <param name="options">The options.</param>
  583. /// <param name="cancellationToken">The cancellation token.</param>
  584. /// <returns>
  585. /// The distinct values for the specified field.
  586. /// </returns>
  587. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, TField>> field, FilterDefinition<TDocument> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  588. {
  589. Ensure.IsNotNull(collection, nameof(collection));
  590. Ensure.IsNotNull(field, nameof(field));
  591. Ensure.IsNotNull(filter, nameof(filter));
  592. return collection.DistinctAsync(
  593. new ExpressionFieldDefinition<TDocument, TField>(field),
  594. filter,
  595. options,
  596. cancellationToken);
  597. }
  598. /// <summary>
  599. /// Gets the distinct values for a specified field.
  600. /// </summary>
  601. /// <typeparam name="TDocument">The type of the document.</typeparam>
  602. /// <typeparam name="TField">The type of the result.</typeparam>
  603. /// <param name="collection">The collection.</param>
  604. /// <param name="field">The field.</param>
  605. /// <param name="filter">The filter.</param>
  606. /// <param name="options">The options.</param>
  607. /// <param name="cancellationToken">The cancellation token.</param>
  608. /// <returns>
  609. /// The distinct values for the specified field.
  610. /// </returns>
  611. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, FieldDefinition<TDocument, TField> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  612. {
  613. Ensure.IsNotNull(collection, nameof(collection));
  614. Ensure.IsNotNull(field, nameof(field));
  615. Ensure.IsNotNull(filter, nameof(filter));
  616. return collection.DistinctAsync(
  617. field,
  618. new ExpressionFilterDefinition<TDocument>(filter),
  619. options,
  620. cancellationToken);
  621. }
  622. /// <summary>
  623. /// Gets the distinct values for a specified field.
  624. /// </summary>
  625. /// <typeparam name="TDocument">The type of the document.</typeparam>
  626. /// <typeparam name="TField">The type of the result.</typeparam>
  627. /// <param name="collection">The collection.</param>
  628. /// <param name="field">The field.</param>
  629. /// <param name="filter">The filter.</param>
  630. /// <param name="options">The options.</param>
  631. /// <param name="cancellationToken">The cancellation token.</param>
  632. /// <returns>
  633. /// The distinct values for the specified field.
  634. /// </returns>
  635. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, TField>> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  636. {
  637. Ensure.IsNotNull(collection, nameof(collection));
  638. Ensure.IsNotNull(field, nameof(field));
  639. Ensure.IsNotNull(filter, nameof(filter));
  640. return collection.DistinctAsync(
  641. new ExpressionFieldDefinition<TDocument, TField>(field),
  642. new ExpressionFilterDefinition<TDocument>(filter),
  643. options,
  644. cancellationToken);
  645. }
  646. /// <summary>
  647. /// Gets the distinct values for a specified field.
  648. /// </summary>
  649. /// <typeparam name="TDocument">The type of the document.</typeparam>
  650. /// <typeparam name="TField">The type of the result.</typeparam>
  651. /// <param name="collection">The collection.</param>
  652. /// <param name="session">The session.</param>
  653. /// <param name="field">The field.</param>
  654. /// <param name="filter">The filter.</param>
  655. /// <param name="options">The options.</param>
  656. /// <param name="cancellationToken">The cancellation token.</param>
  657. /// <returns>
  658. /// The distinct values for the specified field.
  659. /// </returns>
  660. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, TField>> field, FilterDefinition<TDocument> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  661. {
  662. Ensure.IsNotNull(collection, nameof(collection));
  663. Ensure.IsNotNull(session, nameof(session));
  664. Ensure.IsNotNull(field, nameof(field));
  665. Ensure.IsNotNull(filter, nameof(filter));
  666. return collection.DistinctAsync(
  667. session,
  668. new ExpressionFieldDefinition<TDocument, TField>(field),
  669. filter,
  670. options,
  671. cancellationToken);
  672. }
  673. /// <summary>
  674. /// Gets the distinct values for a specified field.
  675. /// </summary>
  676. /// <typeparam name="TDocument">The type of the document.</typeparam>
  677. /// <typeparam name="TField">The type of the result.</typeparam>
  678. /// <param name="collection">The collection.</param>
  679. /// <param name="session">The session.</param>
  680. /// <param name="field">The field.</param>
  681. /// <param name="filter">The filter.</param>
  682. /// <param name="options">The options.</param>
  683. /// <param name="cancellationToken">The cancellation token.</param>
  684. /// <returns>
  685. /// The distinct values for the specified field.
  686. /// </returns>
  687. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FieldDefinition<TDocument, TField> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  688. {
  689. Ensure.IsNotNull(collection, nameof(collection));
  690. Ensure.IsNotNull(session, nameof(session));
  691. Ensure.IsNotNull(field, nameof(field));
  692. Ensure.IsNotNull(filter, nameof(filter));
  693. return collection.DistinctAsync(
  694. session,
  695. field,
  696. new ExpressionFilterDefinition<TDocument>(filter),
  697. options,
  698. cancellationToken);
  699. }
  700. /// <summary>
  701. /// Gets the distinct values for a specified field.
  702. /// </summary>
  703. /// <typeparam name="TDocument">The type of the document.</typeparam>
  704. /// <typeparam name="TField">The type of the result.</typeparam>
  705. /// <param name="collection">The collection.</param>
  706. /// <param name="session">The session.</param>
  707. /// <param name="field">The field.</param>
  708. /// <param name="filter">The filter.</param>
  709. /// <param name="options">The options.</param>
  710. /// <param name="cancellationToken">The cancellation token.</param>
  711. /// <returns>
  712. /// The distinct values for the specified field.
  713. /// </returns>
  714. public static Task<IAsyncCursor<TField>> DistinctAsync<TDocument, TField>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, TField>> field, Expression<Func<TDocument, bool>> filter, DistinctOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  715. {
  716. Ensure.IsNotNull(collection, nameof(collection));
  717. Ensure.IsNotNull(session, nameof(session));
  718. Ensure.IsNotNull(field, nameof(field));
  719. Ensure.IsNotNull(filter, nameof(filter));
  720. return collection.DistinctAsync(
  721. session,
  722. new ExpressionFieldDefinition<TDocument, TField>(field),
  723. new ExpressionFilterDefinition<TDocument>(filter),
  724. options,
  725. cancellationToken);
  726. }
  727. /// <summary>
  728. /// Begins a fluent find interface.
  729. /// </summary>
  730. /// <typeparam name="TDocument">The type of the document.</typeparam>
  731. /// <param name="collection">The collection.</param>
  732. /// <param name="filter">The filter.</param>
  733. /// <param name="options">The options.</param>
  734. /// <returns>
  735. /// A fluent find interface.
  736. /// </returns>
  737. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions options = null)
  738. {
  739. return FindHelper(null, collection, filter, options);
  740. }
  741. /// <summary>
  742. /// Begins a fluent find interface.
  743. /// </summary>
  744. /// <typeparam name="TDocument">The type of the document.</typeparam>
  745. /// <param name="collection">The collection.</param>
  746. /// <param name="session">The session.</param>
  747. /// <param name="filter">The filter.</param>
  748. /// <param name="options">The options.</param>
  749. /// <returns>
  750. /// A fluent find interface.
  751. /// </returns>
  752. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOptions options = null)
  753. {
  754. Ensure.IsNotNull(session, nameof(session));
  755. return FindHelper(session, collection, filter, options);
  756. }
  757. private static IFindFluent<TDocument, TDocument> FindHelper<TDocument>(IClientSessionHandle session, IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions options)
  758. {
  759. FindOptions<TDocument, TDocument> genericOptions;
  760. if (options == null)
  761. {
  762. genericOptions = new FindOptions<TDocument>();
  763. }
  764. else
  765. {
  766. genericOptions = new FindOptions<TDocument>
  767. {
  768. AllowPartialResults = options.AllowPartialResults,
  769. BatchSize = options.BatchSize,
  770. Collation = options.Collation,
  771. Comment = options.Comment,
  772. CursorType = options.CursorType,
  773. MaxAwaitTime = options.MaxAwaitTime,
  774. MaxTime = options.MaxTime,
  775. Modifiers = options.Modifiers,
  776. NoCursorTimeout = options.NoCursorTimeout,
  777. OplogReplay = options.OplogReplay
  778. };
  779. }
  780. return new FindFluent<TDocument, TDocument>(session, collection, filter, genericOptions);
  781. }
  782. /// <summary>
  783. /// Begins a fluent find interface.
  784. /// </summary>
  785. /// <typeparam name="TDocument">The type of the document.</typeparam>
  786. /// <param name="collection">The collection.</param>
  787. /// <param name="filter">The filter.</param>
  788. /// <param name="options">The options.</param>
  789. /// <returns>
  790. /// A fluent interface.
  791. /// </returns>
  792. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions options = null)
  793. {
  794. Ensure.IsNotNull(collection, nameof(collection));
  795. Ensure.IsNotNull(filter, nameof(filter));
  796. return collection.Find(new ExpressionFilterDefinition<TDocument>(filter), options);
  797. }
  798. /// <summary>
  799. /// Begins a fluent find interface.
  800. /// </summary>
  801. /// <typeparam name="TDocument">The type of the document.</typeparam>
  802. /// <param name="collection">The collection.</param>
  803. /// <param name="session">The session.</param>
  804. /// <param name="filter">The filter.</param>
  805. /// <param name="options">The options.</param>
  806. /// <returns>
  807. /// A fluent interface.
  808. /// </returns>
  809. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOptions options = null)
  810. {
  811. Ensure.IsNotNull(collection, nameof(collection));
  812. Ensure.IsNotNull(session, nameof(session));
  813. Ensure.IsNotNull(filter, nameof(filter));
  814. return collection.Find(session, new ExpressionFilterDefinition<TDocument>(filter), options);
  815. }
  816. /// <summary>
  817. /// Finds the documents matching the filter.
  818. /// </summary>
  819. /// <typeparam name="TDocument">The type of the document.</typeparam>
  820. /// <param name="collection">The collection.</param>
  821. /// <param name="filter">The filter.</param>
  822. /// <param name="options">The options.</param>
  823. /// <param name="cancellationToken">The cancellation token.</param>
  824. /// <returns>A Task whose result is a cursor.</returns>
  825. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  826. {
  827. Ensure.IsNotNull(collection, nameof(collection));
  828. Ensure.IsNotNull(filter, nameof(filter));
  829. return collection.FindSync<TDocument>(filter, options, cancellationToken);
  830. }
  831. /// <summary>
  832. /// Finds the documents matching the filter.
  833. /// </summary>
  834. /// <typeparam name="TDocument">The type of the document.</typeparam>
  835. /// <param name="collection">The collection.</param>
  836. /// <param name="filter">The filter.</param>
  837. /// <param name="options">The options.</param>
  838. /// <param name="cancellationToken">The cancellation token.</param>
  839. /// <returns>A Task whose result is a cursor.</returns>
  840. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  841. {
  842. Ensure.IsNotNull(collection, nameof(collection));
  843. Ensure.IsNotNull(filter, nameof(filter));
  844. return collection.FindSync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  845. }
  846. /// <summary>
  847. /// Finds the documents matching the filter.
  848. /// </summary>
  849. /// <typeparam name="TDocument">The type of the document.</typeparam>
  850. /// <param name="collection">The collection.</param>
  851. /// <param name="session">The session.</param>
  852. /// <param name="filter">The filter.</param>
  853. /// <param name="options">The options.</param>
  854. /// <param name="cancellationToken">The cancellation token.</param>
  855. /// <returns>
  856. /// A Task whose result is a cursor.
  857. /// </returns>
  858. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  859. {
  860. Ensure.IsNotNull(collection, nameof(collection));
  861. Ensure.IsNotNull(session, nameof(session));
  862. Ensure.IsNotNull(filter, nameof(filter));
  863. return collection.FindSync<TDocument>(session, filter, options, cancellationToken);
  864. }
  865. /// <summary>
  866. /// Finds the documents matching the filter.
  867. /// </summary>
  868. /// <typeparam name="TDocument">The type of the document.</typeparam>
  869. /// <param name="collection">The collection.</param>
  870. /// <param name="session">The session.</param>
  871. /// <param name="filter">The filter.</param>
  872. /// <param name="options">The options.</param>
  873. /// <param name="cancellationToken">The cancellation token.</param>
  874. /// <returns>
  875. /// A Task whose result is a cursor.
  876. /// </returns>
  877. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  878. {
  879. Ensure.IsNotNull(collection, nameof(collection));
  880. Ensure.IsNotNull(session, nameof(session));
  881. Ensure.IsNotNull(filter, nameof(filter));
  882. return collection.FindSync<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  883. }
  884. /// <summary>
  885. /// Finds the documents matching the filter.
  886. /// </summary>
  887. /// <typeparam name="TDocument">The type of the document.</typeparam>
  888. /// <param name="collection">The collection.</param>
  889. /// <param name="filter">The filter.</param>
  890. /// <param name="options">The options.</param>
  891. /// <param name="cancellationToken">The cancellation token.</param>
  892. /// <returns>A Task whose result is a cursor.</returns>
  893. public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  894. {
  895. Ensure.IsNotNull(collection, nameof(collection));
  896. Ensure.IsNotNull(filter, nameof(filter));
  897. return collection.FindAsync<TDocument>(filter, options, cancellationToken);
  898. }
  899. /// <summary>
  900. /// Finds the documents matching the filter.
  901. /// </summary>
  902. /// <typeparam name="TDocument">The type of the document.</typeparam>
  903. /// <param name="collection">The collection.</param>
  904. /// <param name="filter">The filter.</param>
  905. /// <param name="options">The options.</param>
  906. /// <param name="cancellationToken">The cancellation token.</param>
  907. /// <returns>A Task whose result is a cursor.</returns>
  908. public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  909. {
  910. Ensure.IsNotNull(collection, nameof(collection));
  911. Ensure.IsNotNull(filter, nameof(filter));
  912. return collection.FindAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  913. }
  914. /// <summary>
  915. /// Finds the documents matching the filter.
  916. /// </summary>
  917. /// <typeparam name="TDocument">The type of the document.</typeparam>
  918. /// <param name="collection">The collection.</param>
  919. /// <param name="session">The session.</param>
  920. /// <param name="filter">The filter.</param>
  921. /// <param name="options">The options.</param>
  922. /// <param name="cancellationToken">The cancellation token.</param>
  923. /// <returns>
  924. /// A Task whose result is a cursor.
  925. /// </returns>
  926. public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  927. {
  928. Ensure.IsNotNull(collection, nameof(collection));
  929. Ensure.IsNotNull(session, nameof(session));
  930. Ensure.IsNotNull(filter, nameof(filter));
  931. return collection.FindAsync<TDocument>(session, filter, options, cancellationToken);
  932. }
  933. /// <summary>
  934. /// Finds the documents matching the filter.
  935. /// </summary>
  936. /// <typeparam name="TDocument">The type of the document.</typeparam>
  937. /// <param name="collection">The collection.</param>
  938. /// <param name="session">The session.</param>
  939. /// <param name="filter">The filter.</param>
  940. /// <param name="options">The options.</param>
  941. /// <param name="cancellationToken">The cancellation token.</param>
  942. /// <returns>
  943. /// A Task whose result is a cursor.
  944. /// </returns>
  945. public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  946. {
  947. Ensure.IsNotNull(collection, nameof(collection));
  948. Ensure.IsNotNull(session, nameof(session));
  949. Ensure.IsNotNull(filter, nameof(filter));
  950. return collection.FindAsync<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  951. }
  952. /// <summary>
  953. /// Finds a single document and deletes it atomically.
  954. /// </summary>
  955. /// <typeparam name="TDocument">The type of the document.</typeparam>
  956. /// <param name="collection">The collection.</param>
  957. /// <param name="filter">The filter.</param>
  958. /// <param name="options">The options.</param>
  959. /// <param name="cancellationToken">The cancellation token.</param>
  960. /// <returns>
  961. /// The deleted document if one was deleted.
  962. /// </returns>
  963. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  964. {
  965. Ensure.IsNotNull(collection, nameof(collection));
  966. Ensure.IsNotNull(filter, nameof(filter));
  967. return collection.FindOneAndDelete<TDocument>(filter, options, cancellationToken);
  968. }
  969. /// <summary>
  970. /// Finds a single document and deletes it atomically.
  971. /// </summary>
  972. /// <typeparam name="TDocument">The type of the document.</typeparam>
  973. /// <param name="collection">The collection.</param>
  974. /// <param name="filter">The filter.</param>
  975. /// <param name="options">The options.</param>
  976. /// <param name="cancellationToken">The cancellation token.</param>
  977. /// <returns>
  978. /// The deleted document if one was deleted.
  979. /// </returns>
  980. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  981. {
  982. Ensure.IsNotNull(collection, nameof(collection));
  983. Ensure.IsNotNull(filter, nameof(filter));
  984. return collection.FindOneAndDelete<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  985. }
  986. /// <summary>
  987. /// Finds a single document and deletes it atomically.
  988. /// </summary>
  989. /// <typeparam name="TDocument">The type of the document.</typeparam>
  990. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  991. /// <param name="collection">The collection.</param>
  992. /// <param name="filter">The filter.</param>
  993. /// <param name="options">The options.</param>
  994. /// <param name="cancellationToken">The cancellation token.</param>
  995. /// <returns>
  996. /// The returned document.
  997. /// </returns>
  998. public static TProjection FindOneAndDelete<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  999. {
  1000. Ensure.IsNotNull(collection, nameof(collection));
  1001. Ensure.IsNotNull(filter, nameof(filter));
  1002. return collection.FindOneAndDelete<TProjection>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1003. }
  1004. /// <summary>
  1005. /// Finds a single document and deletes it atomically.
  1006. /// </summary>
  1007. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1008. /// <param name="collection">The collection.</param>
  1009. /// <param name="session">The session.</param>
  1010. /// <param name="filter">The filter.</param>
  1011. /// <param name="options">The options.</param>
  1012. /// <param name="cancellationToken">The cancellation token.</param>
  1013. /// <returns>
  1014. /// The deleted document if one was deleted.
  1015. /// </returns>
  1016. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1017. {
  1018. Ensure.IsNotNull(collection, nameof(collection));
  1019. Ensure.IsNotNull(session, nameof(session));
  1020. Ensure.IsNotNull(filter, nameof(filter));
  1021. return collection.FindOneAndDelete<TDocument>(session, filter, options, cancellationToken);
  1022. }
  1023. /// <summary>
  1024. /// Finds a single document and deletes it atomically.
  1025. /// </summary>
  1026. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1027. /// <param name="collection">The collection.</param>
  1028. /// <param name="session">The session.</param>
  1029. /// <param name="filter">The filter.</param>
  1030. /// <param name="options">The options.</param>
  1031. /// <param name="cancellationToken">The cancellation token.</param>
  1032. /// <returns>
  1033. /// The deleted document if one was deleted.
  1034. /// </returns>
  1035. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1036. {
  1037. Ensure.IsNotNull(collection, nameof(collection));
  1038. Ensure.IsNotNull(session, nameof(session));
  1039. Ensure.IsNotNull(filter, nameof(filter));
  1040. return collection.FindOneAndDelete<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1041. }
  1042. /// <summary>
  1043. /// Finds a single document and deletes it atomically.
  1044. /// </summary>
  1045. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1046. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1047. /// <param name="collection">The collection.</param>
  1048. /// <param name="session">The session.</param>
  1049. /// <param name="filter">The filter.</param>
  1050. /// <param name="options">The options.</param>
  1051. /// <param name="cancellationToken">The cancellation token.</param>
  1052. /// <returns>
  1053. /// The returned document.
  1054. /// </returns>
  1055. public static TProjection FindOneAndDelete<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1056. {
  1057. Ensure.IsNotNull(collection, nameof(collection));
  1058. Ensure.IsNotNull(session, nameof(session));
  1059. Ensure.IsNotNull(filter, nameof(filter));
  1060. return collection.FindOneAndDelete<TProjection>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1061. }
  1062. /// <summary>
  1063. /// Finds a single document and deletes it atomically.
  1064. /// </summary>
  1065. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1066. /// <param name="collection">The collection.</param>
  1067. /// <param name="filter">The filter.</param>
  1068. /// <param name="options">The options.</param>
  1069. /// <param name="cancellationToken">The cancellation token.</param>
  1070. /// <returns>
  1071. /// The deleted document if one was deleted.
  1072. /// </returns>
  1073. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1074. {
  1075. Ensure.IsNotNull(collection, nameof(collection));
  1076. Ensure.IsNotNull(filter, nameof(filter));
  1077. return collection.FindOneAndDeleteAsync<TDocument>(filter, options, cancellationToken);
  1078. }
  1079. /// <summary>
  1080. /// Finds a single document and deletes it atomically.
  1081. /// </summary>
  1082. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1083. /// <param name="collection">The collection.</param>
  1084. /// <param name="filter">The filter.</param>
  1085. /// <param name="options">The options.</param>
  1086. /// <param name="cancellationToken">The cancellation token.</param>
  1087. /// <returns>
  1088. /// The deleted document if one was deleted.
  1089. /// </returns>
  1090. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1091. {
  1092. Ensure.IsNotNull(collection, nameof(collection));
  1093. Ensure.IsNotNull(filter, nameof(filter));
  1094. return collection.FindOneAndDeleteAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1095. }
  1096. /// <summary>
  1097. /// Finds a single document and deletes it atomically.
  1098. /// </summary>
  1099. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1100. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1101. /// <param name="collection">The collection.</param>
  1102. /// <param name="filter">The filter.</param>
  1103. /// <param name="options">The options.</param>
  1104. /// <param name="cancellationToken">The cancellation token.</param>
  1105. /// <returns>
  1106. /// The returned document.
  1107. /// </returns>
  1108. public static Task<TProjection> FindOneAndDeleteAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1109. {
  1110. Ensure.IsNotNull(collection, nameof(collection));
  1111. Ensure.IsNotNull(filter, nameof(filter));
  1112. return collection.FindOneAndDeleteAsync<TProjection>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1113. }
  1114. /// <summary>
  1115. /// Finds a single document and deletes it atomically.
  1116. /// </summary>
  1117. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1118. /// <param name="collection">The collection.</param>
  1119. /// <param name="session">The session.</param>
  1120. /// <param name="filter">The filter.</param>
  1121. /// <param name="options">The options.</param>
  1122. /// <param name="cancellationToken">The cancellation token.</param>
  1123. /// <returns>
  1124. /// The deleted document if one was deleted.
  1125. /// </returns>
  1126. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1127. {
  1128. Ensure.IsNotNull(collection, nameof(collection));
  1129. Ensure.IsNotNull(session, nameof(session));
  1130. Ensure.IsNotNull(filter, nameof(filter));
  1131. return collection.FindOneAndDeleteAsync<TDocument>(session, filter, options, cancellationToken);
  1132. }
  1133. /// <summary>
  1134. /// Finds a single document and deletes it atomically.
  1135. /// </summary>
  1136. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1137. /// <param name="collection">The collection.</param>
  1138. /// <param name="session">The session.</param>
  1139. /// <param name="filter">The filter.</param>
  1140. /// <param name="options">The options.</param>
  1141. /// <param name="cancellationToken">The cancellation token.</param>
  1142. /// <returns>
  1143. /// The deleted document if one was deleted.
  1144. /// </returns>
  1145. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1146. {
  1147. Ensure.IsNotNull(collection, nameof(collection));
  1148. Ensure.IsNotNull(session, nameof(session));
  1149. Ensure.IsNotNull(filter, nameof(filter));
  1150. return collection.FindOneAndDeleteAsync<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1151. }
  1152. /// <summary>
  1153. /// Finds a single document and deletes it atomically.
  1154. /// </summary>
  1155. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1156. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1157. /// <param name="collection">The collection.</param>
  1158. /// <param name="session">The session.</param>
  1159. /// <param name="filter">The filter.</param>
  1160. /// <param name="options">The options.</param>
  1161. /// <param name="cancellationToken">The cancellation token.</param>
  1162. /// <returns>
  1163. /// The returned document.
  1164. /// </returns>
  1165. public static Task<TProjection> FindOneAndDeleteAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1166. {
  1167. Ensure.IsNotNull(collection, nameof(collection));
  1168. Ensure.IsNotNull(session, nameof(session));
  1169. Ensure.IsNotNull(filter, nameof(filter));
  1170. return collection.FindOneAndDeleteAsync<TProjection>(session, new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  1171. }
  1172. /// <summary>
  1173. /// Finds a single document and replaces it atomically.
  1174. /// </summary>
  1175. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1176. /// <param name="collection">The collection.</param>
  1177. /// <param name="filter">The filter.</param>
  1178. /// <param name="replacement">The replacement.</param>
  1179. /// <param name="options">The options.</param>
  1180. /// <param name="cancellationToken">The cancellation token.</param>
  1181. /// <returns>
  1182. /// The returned document.
  1183. /// </returns>
  1184. public static TDocument FindOneAndReplace<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1185. {
  1186. Ensure.IsNotNull(collection, nameof(collection));
  1187. Ensure.IsNotNull(filter, nameof(filter));
  1188. return collection.FindOneAndReplace<TDocument>(filter, replacement, options, cancellationToken);
  1189. }
  1190. /// <summary>
  1191. /// Finds a single document and replaces it atomically.
  1192. /// </summary>
  1193. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1194. /// <param name="collection">The collection.</param>
  1195. /// <param name="filter">The filter.</param>
  1196. /// <param name="replacement">The replacement.</param>
  1197. /// <param name="options">The options.</param>
  1198. /// <param name="cancellationToken">The cancellation token.</param>
  1199. /// <returns>
  1200. /// The returned document.
  1201. /// </returns>
  1202. public static TDocument FindOneAndReplace<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1203. {
  1204. Ensure.IsNotNull(collection, nameof(collection));
  1205. Ensure.IsNotNull(filter, nameof(filter));
  1206. return collection.FindOneAndReplace<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1207. }
  1208. /// <summary>
  1209. /// Finds a single document and replaces it atomically.
  1210. /// </summary>
  1211. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1212. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1213. /// <param name="collection">The collection.</param>
  1214. /// <param name="filter">The filter.</param>
  1215. /// <param name="replacement">The replacement.</param>
  1216. /// <param name="options">The options.</param>
  1217. /// <param name="cancellationToken">The cancellation token.</param>
  1218. /// <returns>
  1219. /// The returned document.
  1220. /// </returns>
  1221. public static TProjection FindOneAndReplace<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1222. {
  1223. Ensure.IsNotNull(collection, nameof(collection));
  1224. Ensure.IsNotNull(filter, nameof(filter));
  1225. return collection.FindOneAndReplace<TProjection>(filter, replacement, options, cancellationToken);
  1226. }
  1227. /// <summary>
  1228. /// Finds a single document and replaces it atomically.
  1229. /// </summary>
  1230. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1231. /// <param name="collection">The collection.</param>
  1232. /// <param name="session">The session.</param>
  1233. /// <param name="filter">The filter.</param>
  1234. /// <param name="replacement">The replacement.</param>
  1235. /// <param name="options">The options.</param>
  1236. /// <param name="cancellationToken">The cancellation token.</param>
  1237. /// <returns>
  1238. /// The returned document.
  1239. /// </returns>
  1240. public static TDocument FindOneAndReplace<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1241. {
  1242. Ensure.IsNotNull(collection, nameof(collection));
  1243. Ensure.IsNotNull(session, nameof(session));
  1244. Ensure.IsNotNull(filter, nameof(filter));
  1245. return collection.FindOneAndReplace<TDocument>(session, filter, replacement, options, cancellationToken);
  1246. }
  1247. /// <summary>
  1248. /// Finds a single document and replaces it atomically.
  1249. /// </summary>
  1250. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1251. /// <param name="collection">The collection.</param>
  1252. /// <param name="session">The session.</param>
  1253. /// <param name="filter">The filter.</param>
  1254. /// <param name="replacement">The replacement.</param>
  1255. /// <param name="options">The options.</param>
  1256. /// <param name="cancellationToken">The cancellation token.</param>
  1257. /// <returns>
  1258. /// The returned document.
  1259. /// </returns>
  1260. public static TDocument FindOneAndReplace<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1261. {
  1262. Ensure.IsNotNull(collection, nameof(collection));
  1263. Ensure.IsNotNull(session, nameof(session));
  1264. Ensure.IsNotNull(filter, nameof(filter));
  1265. return collection.FindOneAndReplace<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1266. }
  1267. /// <summary>
  1268. /// Finds a single document and replaces it atomically.
  1269. /// </summary>
  1270. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1271. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1272. /// <param name="collection">The collection.</param>
  1273. /// <param name="session">The session.</param>
  1274. /// <param name="filter">The filter.</param>
  1275. /// <param name="replacement">The replacement.</param>
  1276. /// <param name="options">The options.</param>
  1277. /// <param name="cancellationToken">The cancellation token.</param>
  1278. /// <returns>
  1279. /// The returned document.
  1280. /// </returns>
  1281. public static TProjection FindOneAndReplace<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1282. {
  1283. Ensure.IsNotNull(collection, nameof(collection));
  1284. Ensure.IsNotNull(session, nameof(session));
  1285. Ensure.IsNotNull(filter, nameof(filter));
  1286. return collection.FindOneAndReplace<TProjection>(session, filter, replacement, options, cancellationToken);
  1287. }
  1288. /// <summary>
  1289. /// Finds a single document and replaces it atomically.
  1290. /// </summary>
  1291. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1292. /// <param name="collection">The collection.</param>
  1293. /// <param name="filter">The filter.</param>
  1294. /// <param name="replacement">The replacement.</param>
  1295. /// <param name="options">The options.</param>
  1296. /// <param name="cancellationToken">The cancellation token.</param>
  1297. /// <returns>
  1298. /// The returned document.
  1299. /// </returns>
  1300. public static Task<TDocument> FindOneAndReplaceAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1301. {
  1302. Ensure.IsNotNull(collection, nameof(collection));
  1303. Ensure.IsNotNull(filter, nameof(filter));
  1304. return collection.FindOneAndReplaceAsync<TDocument>(filter, replacement, options, cancellationToken);
  1305. }
  1306. /// <summary>
  1307. /// Finds a single document and replaces it atomically.
  1308. /// </summary>
  1309. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1310. /// <param name="collection">The collection.</param>
  1311. /// <param name="filter">The filter.</param>
  1312. /// <param name="replacement">The replacement.</param>
  1313. /// <param name="options">The options.</param>
  1314. /// <param name="cancellationToken">The cancellation token.</param>
  1315. /// <returns>
  1316. /// The returned document.
  1317. /// </returns>
  1318. public static Task<TDocument> FindOneAndReplaceAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1319. {
  1320. Ensure.IsNotNull(collection, nameof(collection));
  1321. Ensure.IsNotNull(filter, nameof(filter));
  1322. return collection.FindOneAndReplaceAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1323. }
  1324. /// <summary>
  1325. /// Finds a single document and replaces it atomically.
  1326. /// </summary>
  1327. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1328. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1329. /// <param name="collection">The collection.</param>
  1330. /// <param name="filter">The filter.</param>
  1331. /// <param name="replacement">The replacement.</param>
  1332. /// <param name="options">The options.</param>
  1333. /// <param name="cancellationToken">The cancellation token.</param>
  1334. /// <returns>
  1335. /// The returned document.
  1336. /// </returns>
  1337. public static Task<TProjection> FindOneAndReplaceAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1338. {
  1339. Ensure.IsNotNull(collection, nameof(collection));
  1340. Ensure.IsNotNull(filter, nameof(filter));
  1341. return collection.FindOneAndReplaceAsync<TProjection>(filter, replacement, options, cancellationToken);
  1342. }
  1343. /// <summary>
  1344. /// Finds a single document and replaces it atomically.
  1345. /// </summary>
  1346. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1347. /// <param name="collection">The collection.</param>
  1348. /// <param name="session">The session.</param>
  1349. /// <param name="filter">The filter.</param>
  1350. /// <param name="replacement">The replacement.</param>
  1351. /// <param name="options">The options.</param>
  1352. /// <param name="cancellationToken">The cancellation token.</param>
  1353. /// <returns>
  1354. /// The returned document.
  1355. /// </returns>
  1356. public static Task<TDocument> FindOneAndReplaceAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1357. {
  1358. Ensure.IsNotNull(collection, nameof(collection));
  1359. Ensure.IsNotNull(session, nameof(session));
  1360. Ensure.IsNotNull(filter, nameof(filter));
  1361. return collection.FindOneAndReplaceAsync<TDocument>(session, filter, replacement, options, cancellationToken);
  1362. }
  1363. /// <summary>
  1364. /// Finds a single document and replaces it atomically.
  1365. /// </summary>
  1366. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1367. /// <param name="collection">The collection.</param>
  1368. /// <param name="filter">The filter.</param>
  1369. /// <param name="session">The session.</param>
  1370. /// <param name="replacement">The replacement.</param>
  1371. /// <param name="options">The options.</param>
  1372. /// <param name="cancellationToken">The cancellation token.</param>
  1373. /// <returns>
  1374. /// The returned document.
  1375. /// </returns>
  1376. public static Task<TDocument> FindOneAndReplaceAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, IClientSessionHandle session, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1377. {
  1378. Ensure.IsNotNull(collection, nameof(collection));
  1379. Ensure.IsNotNull(session, nameof(session));
  1380. Ensure.IsNotNull(filter, nameof(filter));
  1381. return collection.FindOneAndReplaceAsync<TDocument>(session, new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1382. }
  1383. /// <summary>
  1384. /// Finds a single document and replaces it atomically.
  1385. /// </summary>
  1386. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1387. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1388. /// <param name="collection">The collection.</param>
  1389. /// <param name="session">The session.</param>
  1390. /// <param name="filter">The filter.</param>
  1391. /// <param name="replacement">The replacement.</param>
  1392. /// <param name="options">The options.</param>
  1393. /// <param name="cancellationToken">The cancellation token.</param>
  1394. /// <returns>
  1395. /// The returned document.
  1396. /// </returns>
  1397. public static Task<TProjection> FindOneAndReplaceAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1398. {
  1399. Ensure.IsNotNull(collection, nameof(collection));
  1400. Ensure.IsNotNull(session, nameof(session));
  1401. Ensure.IsNotNull(filter, nameof(filter));
  1402. return collection.FindOneAndReplaceAsync<TProjection>(session, filter, replacement, options, cancellationToken);
  1403. }
  1404. /// <summary>
  1405. /// Finds a single document and updates it atomically.
  1406. /// </summary>
  1407. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1408. /// <param name="collection">The collection.</param>
  1409. /// <param name="filter">The filter.</param>
  1410. /// <param name="update">The update.</param>
  1411. /// <param name="options">The options.</param>
  1412. /// <param name="cancellationToken">The cancellation token.</param>
  1413. /// <returns>
  1414. /// The returned document.
  1415. /// </returns>
  1416. public static TDocument FindOneAndUpdate<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1417. {
  1418. Ensure.IsNotNull(collection, nameof(collection));
  1419. Ensure.IsNotNull(filter, nameof(filter));
  1420. Ensure.IsNotNull(update, nameof(update));
  1421. return collection.FindOneAndUpdate<TDocument>(
  1422. filter,
  1423. update,
  1424. options,
  1425. cancellationToken);
  1426. }
  1427. /// <summary>
  1428. /// Finds a single document and updates it atomically.
  1429. /// </summary>
  1430. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1431. /// <param name="collection">The collection.</param>
  1432. /// <param name="filter">The filter.</param>
  1433. /// <param name="update">The update.</param>
  1434. /// <param name="options">The options.</param>
  1435. /// <param name="cancellationToken">The cancellation token.</param>
  1436. /// <returns>
  1437. /// The returned document.
  1438. /// </returns>
  1439. public static TDocument FindOneAndUpdate<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1440. {
  1441. Ensure.IsNotNull(collection, nameof(collection));
  1442. Ensure.IsNotNull(filter, nameof(filter));
  1443. Ensure.IsNotNull(update, nameof(update));
  1444. return collection.FindOneAndUpdate<TDocument>(
  1445. new ExpressionFilterDefinition<TDocument>(filter),
  1446. update,
  1447. options,
  1448. cancellationToken);
  1449. }
  1450. /// <summary>
  1451. /// Finds a single document and updates it atomically.
  1452. /// </summary>
  1453. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1454. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1455. /// <param name="collection">The collection.</param>
  1456. /// <param name="filter">The filter.</param>
  1457. /// <param name="update">The update.</param>
  1458. /// <param name="options">The options.</param>
  1459. /// <param name="cancellationToken">The cancellation token.</param>
  1460. /// <returns>
  1461. /// The returned document.
  1462. /// </returns>
  1463. public static TProjection FindOneAndUpdate<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1464. {
  1465. Ensure.IsNotNull(collection, nameof(collection));
  1466. Ensure.IsNotNull(filter, nameof(filter));
  1467. Ensure.IsNotNull(update, nameof(update));
  1468. return collection.FindOneAndUpdate(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1469. }
  1470. /// <summary>
  1471. /// Finds a single document and updates it atomically.
  1472. /// </summary>
  1473. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1474. /// <param name="collection">The collection.</param>
  1475. /// <param name="session">The session.</param>
  1476. /// <param name="filter">The filter.</param>
  1477. /// <param name="update">The update.</param>
  1478. /// <param name="options">The options.</param>
  1479. /// <param name="cancellationToken">The cancellation token.</param>
  1480. /// <returns>
  1481. /// The returned document.
  1482. /// </returns>
  1483. public static TDocument FindOneAndUpdate<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1484. {
  1485. Ensure.IsNotNull(collection, nameof(collection));
  1486. Ensure.IsNotNull(session, nameof(session));
  1487. Ensure.IsNotNull(filter, nameof(filter));
  1488. Ensure.IsNotNull(update, nameof(update));
  1489. return collection.FindOneAndUpdate<TDocument>(
  1490. session,
  1491. filter,
  1492. update,
  1493. options,
  1494. cancellationToken);
  1495. }
  1496. /// <summary>
  1497. /// Finds a single document and updates it atomically.
  1498. /// </summary>
  1499. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1500. /// <param name="collection">The collection.</param>
  1501. /// <param name="session">The session.</param>
  1502. /// <param name="filter">The filter.</param>
  1503. /// <param name="update">The update.</param>
  1504. /// <param name="options">The options.</param>
  1505. /// <param name="cancellationToken">The cancellation token.</param>
  1506. /// <returns>
  1507. /// The returned document.
  1508. /// </returns>
  1509. public static TDocument FindOneAndUpdate<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1510. {
  1511. Ensure.IsNotNull(collection, nameof(collection));
  1512. Ensure.IsNotNull(session, nameof(session));
  1513. Ensure.IsNotNull(filter, nameof(filter));
  1514. Ensure.IsNotNull(update, nameof(update));
  1515. return collection.FindOneAndUpdate<TDocument>(
  1516. session,
  1517. new ExpressionFilterDefinition<TDocument>(filter),
  1518. update,
  1519. options,
  1520. cancellationToken);
  1521. }
  1522. /// <summary>
  1523. /// Finds a single document and updates it atomically.
  1524. /// </summary>
  1525. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1526. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1527. /// <param name="collection">The collection.</param>
  1528. /// <param name="session">The session.</param>
  1529. /// <param name="filter">The filter.</param>
  1530. /// <param name="update">The update.</param>
  1531. /// <param name="options">The options.</param>
  1532. /// <param name="cancellationToken">The cancellation token.</param>
  1533. /// <returns>
  1534. /// The returned document.
  1535. /// </returns>
  1536. public static TProjection FindOneAndUpdate<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1537. {
  1538. Ensure.IsNotNull(collection, nameof(collection));
  1539. Ensure.IsNotNull(session, nameof(session));
  1540. Ensure.IsNotNull(filter, nameof(filter));
  1541. Ensure.IsNotNull(update, nameof(update));
  1542. return collection.FindOneAndUpdate(session, new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1543. }
  1544. /// <summary>
  1545. /// Finds a single document and updates it atomically.
  1546. /// </summary>
  1547. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1548. /// <param name="collection">The collection.</param>
  1549. /// <param name="filter">The filter.</param>
  1550. /// <param name="update">The update.</param>
  1551. /// <param name="options">The options.</param>
  1552. /// <param name="cancellationToken">The cancellation token.</param>
  1553. /// <returns>
  1554. /// The returned document.
  1555. /// </returns>
  1556. public static Task<TDocument> FindOneAndUpdateAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1557. {
  1558. Ensure.IsNotNull(collection, nameof(collection));
  1559. Ensure.IsNotNull(filter, nameof(filter));
  1560. Ensure.IsNotNull(update, nameof(update));
  1561. return collection.FindOneAndUpdateAsync<TDocument>(
  1562. filter,
  1563. update,
  1564. options,
  1565. cancellationToken);
  1566. }
  1567. /// <summary>
  1568. /// Finds a single document and updates it atomically.
  1569. /// </summary>
  1570. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1571. /// <param name="collection">The collection.</param>
  1572. /// <param name="filter">The filter.</param>
  1573. /// <param name="update">The update.</param>
  1574. /// <param name="options">The options.</param>
  1575. /// <param name="cancellationToken">The cancellation token.</param>
  1576. /// <returns>
  1577. /// The returned document.
  1578. /// </returns>
  1579. public static Task<TDocument> FindOneAndUpdateAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1580. {
  1581. Ensure.IsNotNull(collection, nameof(collection));
  1582. Ensure.IsNotNull(filter, nameof(filter));
  1583. Ensure.IsNotNull(update, nameof(update));
  1584. return collection.FindOneAndUpdateAsync<TDocument>(
  1585. new ExpressionFilterDefinition<TDocument>(filter),
  1586. update,
  1587. options,
  1588. cancellationToken);
  1589. }
  1590. /// <summary>
  1591. /// Finds a single document and updates it atomically.
  1592. /// </summary>
  1593. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1594. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1595. /// <param name="collection">The collection.</param>
  1596. /// <param name="filter">The filter.</param>
  1597. /// <param name="update">The update.</param>
  1598. /// <param name="options">The options.</param>
  1599. /// <param name="cancellationToken">The cancellation token.</param>
  1600. /// <returns>
  1601. /// The returned document.
  1602. /// </returns>
  1603. public static Task<TProjection> FindOneAndUpdateAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1604. {
  1605. Ensure.IsNotNull(collection, nameof(collection));
  1606. Ensure.IsNotNull(filter, nameof(filter));
  1607. Ensure.IsNotNull(update, nameof(update));
  1608. return collection.FindOneAndUpdateAsync(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1609. }
  1610. /// <summary>
  1611. /// Finds a single document and updates it atomically.
  1612. /// </summary>
  1613. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1614. /// <param name="collection">The collection.</param>
  1615. /// <param name="session">The session.</param>
  1616. /// <param name="filter">The filter.</param>
  1617. /// <param name="update">The update.</param>
  1618. /// <param name="options">The options.</param>
  1619. /// <param name="cancellationToken">The cancellation token.</param>
  1620. /// <returns>
  1621. /// The returned document.
  1622. /// </returns>
  1623. public static Task<TDocument> FindOneAndUpdateAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1624. {
  1625. Ensure.IsNotNull(collection, nameof(collection));
  1626. Ensure.IsNotNull(session, nameof(session));
  1627. Ensure.IsNotNull(filter, nameof(filter));
  1628. Ensure.IsNotNull(update, nameof(update));
  1629. return collection.FindOneAndUpdateAsync<TDocument>(
  1630. session,
  1631. filter,
  1632. update,
  1633. options,
  1634. cancellationToken);
  1635. }
  1636. /// <summary>
  1637. /// Finds a single document and updates it atomically.
  1638. /// </summary>
  1639. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1640. /// <param name="collection">The collection.</param>
  1641. /// <param name="session">The session.</param>
  1642. /// <param name="filter">The filter.</param>
  1643. /// <param name="update">The update.</param>
  1644. /// <param name="options">The options.</param>
  1645. /// <param name="cancellationToken">The cancellation token.</param>
  1646. /// <returns>
  1647. /// The returned document.
  1648. /// </returns>
  1649. public static Task<TDocument> FindOneAndUpdateAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1650. {
  1651. Ensure.IsNotNull(collection, nameof(collection));
  1652. Ensure.IsNotNull(session, nameof(session));
  1653. Ensure.IsNotNull(filter, nameof(filter));
  1654. Ensure.IsNotNull(update, nameof(update));
  1655. return collection.FindOneAndUpdateAsync<TDocument>(
  1656. session,
  1657. new ExpressionFilterDefinition<TDocument>(filter),
  1658. update,
  1659. options,
  1660. cancellationToken);
  1661. }
  1662. /// <summary>
  1663. /// Finds a single document and updates it atomically.
  1664. /// </summary>
  1665. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1666. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  1667. /// <param name="collection">The collection.</param>
  1668. /// <param name="session">The session.</param>
  1669. /// <param name="filter">The filter.</param>
  1670. /// <param name="update">The update.</param>
  1671. /// <param name="options">The options.</param>
  1672. /// <param name="cancellationToken">The cancellation token.</param>
  1673. /// <returns>
  1674. /// The returned document.
  1675. /// </returns>
  1676. public static Task<TProjection> FindOneAndUpdateAsync<TDocument, TProjection>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  1677. {
  1678. Ensure.IsNotNull(collection, nameof(collection));
  1679. Ensure.IsNotNull(session, nameof(session));
  1680. Ensure.IsNotNull(filter, nameof(filter));
  1681. Ensure.IsNotNull(update, nameof(update));
  1682. return collection.FindOneAndUpdateAsync(session, new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1683. }
  1684. /// <summary>
  1685. /// Replaces a single document.
  1686. /// </summary>
  1687. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1688. /// <param name="collection">The collection.</param>
  1689. /// <param name="filter">The filter.</param>
  1690. /// <param name="replacement">The replacement.</param>
  1691. /// <param name="options">The options.</param>
  1692. /// <param name="cancellationToken">The cancellation token.</param>
  1693. /// <returns>
  1694. /// The result of the replacement.
  1695. /// </returns>
  1696. public static ReplaceOneResult ReplaceOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1697. {
  1698. Ensure.IsNotNull(collection, nameof(collection));
  1699. Ensure.IsNotNull(filter, nameof(filter));
  1700. return collection.ReplaceOne(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1701. }
  1702. /// <summary>
  1703. /// Replaces a single document.
  1704. /// </summary>
  1705. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1706. /// <param name="collection">The collection.</param>
  1707. /// <param name="session">The session.</param>
  1708. /// <param name="filter">The filter.</param>
  1709. /// <param name="replacement">The replacement.</param>
  1710. /// <param name="options">The options.</param>
  1711. /// <param name="cancellationToken">The cancellation token.</param>
  1712. /// <returns>
  1713. /// The result of the replacement.
  1714. /// </returns>
  1715. public static ReplaceOneResult ReplaceOne<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1716. {
  1717. Ensure.IsNotNull(collection, nameof(collection));
  1718. Ensure.IsNotNull(session, nameof(session));
  1719. Ensure.IsNotNull(filter, nameof(filter));
  1720. return collection.ReplaceOne(session, new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1721. }
  1722. /// <summary>
  1723. /// Replaces a single document.
  1724. /// </summary>
  1725. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1726. /// <param name="collection">The collection.</param>
  1727. /// <param name="filter">The filter.</param>
  1728. /// <param name="replacement">The replacement.</param>
  1729. /// <param name="options">The options.</param>
  1730. /// <param name="cancellationToken">The cancellation token.</param>
  1731. /// <returns>
  1732. /// The result of the replacement.
  1733. /// </returns>
  1734. public static Task<ReplaceOneResult> ReplaceOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1735. {
  1736. Ensure.IsNotNull(collection, nameof(collection));
  1737. Ensure.IsNotNull(filter, nameof(filter));
  1738. return collection.ReplaceOneAsync(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1739. }
  1740. /// <summary>
  1741. /// Replaces a single document.
  1742. /// </summary>
  1743. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1744. /// <param name="collection">The collection.</param>
  1745. /// <param name="session">The session.</param>
  1746. /// <param name="filter">The filter.</param>
  1747. /// <param name="replacement">The replacement.</param>
  1748. /// <param name="options">The options.</param>
  1749. /// <param name="cancellationToken">The cancellation token.</param>
  1750. /// <returns>
  1751. /// The result of the replacement.
  1752. /// </returns>
  1753. public static Task<ReplaceOneResult> ReplaceOneAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1754. {
  1755. Ensure.IsNotNull(collection, nameof(collection));
  1756. Ensure.IsNotNull(session, nameof(session));
  1757. Ensure.IsNotNull(filter, nameof(filter));
  1758. return collection.ReplaceOneAsync(session, new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  1759. }
  1760. /// <summary>
  1761. /// Updates many documents.
  1762. /// </summary>
  1763. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1764. /// <param name="collection">The collection.</param>
  1765. /// <param name="filter">The filter.</param>
  1766. /// <param name="update">The update.</param>
  1767. /// <param name="options">The options.</param>
  1768. /// <param name="cancellationToken">The cancellation token.</param>
  1769. /// <returns>
  1770. /// The result of the update operation.
  1771. /// </returns>
  1772. public static UpdateResult UpdateMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1773. {
  1774. Ensure.IsNotNull(collection, nameof(collection));
  1775. Ensure.IsNotNull(filter, nameof(filter));
  1776. return collection.UpdateMany(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1777. }
  1778. /// <summary>
  1779. /// Updates many documents.
  1780. /// </summary>
  1781. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1782. /// <param name="collection">The collection.</param>
  1783. /// <param name="session">The session.</param>
  1784. /// <param name="filter">The filter.</param>
  1785. /// <param name="update">The update.</param>
  1786. /// <param name="options">The options.</param>
  1787. /// <param name="cancellationToken">The cancellation token.</param>
  1788. /// <returns>
  1789. /// The result of the update operation.
  1790. /// </returns>
  1791. public static UpdateResult UpdateMany<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1792. {
  1793. Ensure.IsNotNull(collection, nameof(collection));
  1794. Ensure.IsNotNull(session, nameof(session));
  1795. Ensure.IsNotNull(filter, nameof(filter));
  1796. return collection.UpdateMany(session, new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1797. }
  1798. /// <summary>
  1799. /// Updates many documents.
  1800. /// </summary>
  1801. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1802. /// <param name="collection">The collection.</param>
  1803. /// <param name="filter">The filter.</param>
  1804. /// <param name="update">The update.</param>
  1805. /// <param name="options">The options.</param>
  1806. /// <param name="cancellationToken">The cancellation token.</param>
  1807. /// <returns>
  1808. /// The result of the update operation.
  1809. /// </returns>
  1810. public static Task<UpdateResult> UpdateManyAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1811. {
  1812. Ensure.IsNotNull(collection, nameof(collection));
  1813. Ensure.IsNotNull(filter, nameof(filter));
  1814. return collection.UpdateManyAsync(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1815. }
  1816. /// <summary>
  1817. /// Updates many documents.
  1818. /// </summary>
  1819. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1820. /// <param name="collection">The collection.</param>
  1821. /// <param name="session">The session.</param>
  1822. /// <param name="filter">The filter.</param>
  1823. /// <param name="update">The update.</param>
  1824. /// <param name="options">The options.</param>
  1825. /// <param name="cancellationToken">The cancellation token.</param>
  1826. /// <returns>
  1827. /// The result of the update operation.
  1828. /// </returns>
  1829. public static Task<UpdateResult> UpdateManyAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1830. {
  1831. Ensure.IsNotNull(collection, nameof(collection));
  1832. Ensure.IsNotNull(session, nameof(session));
  1833. Ensure.IsNotNull(filter, nameof(filter));
  1834. return collection.UpdateManyAsync(session, new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  1835. }
  1836. /// <summary>
  1837. /// Updates a single document.
  1838. /// </summary>
  1839. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1840. /// <param name="collection">The collection.</param>
  1841. /// <param name="filter">The filter.</param>
  1842. /// <param name="update">The update.</param>
  1843. /// <param name="options">The options.</param>
  1844. /// <param name="cancellationToken">The cancellation token.</param>
  1845. /// <returns>
  1846. /// The result of the update operation.
  1847. /// </returns>
  1848. public static UpdateResult UpdateOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1849. {
  1850. Ensure.IsNotNull(collection, nameof(collection));
  1851. Ensure.IsNotNull(filter, nameof(filter));
  1852. return collection.UpdateOne(
  1853. new ExpressionFilterDefinition<TDocument>(filter),
  1854. update,
  1855. options,
  1856. cancellationToken);
  1857. }
  1858. /// <summary>
  1859. /// Updates a single document.
  1860. /// </summary>
  1861. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1862. /// <param name="collection">The collection.</param>
  1863. /// <param name="session">The session.</param>
  1864. /// <param name="filter">The filter.</param>
  1865. /// <param name="update">The update.</param>
  1866. /// <param name="options">The options.</param>
  1867. /// <param name="cancellationToken">The cancellation token.</param>
  1868. /// <returns>
  1869. /// The result of the update operation.
  1870. /// </returns>
  1871. public static UpdateResult UpdateOne<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1872. {
  1873. Ensure.IsNotNull(collection, nameof(collection));
  1874. Ensure.IsNotNull(session, nameof(session));
  1875. Ensure.IsNotNull(filter, nameof(filter));
  1876. return collection.UpdateOne(
  1877. session,
  1878. new ExpressionFilterDefinition<TDocument>(filter),
  1879. update,
  1880. options,
  1881. cancellationToken);
  1882. }
  1883. /// <summary>
  1884. /// Updates a single document.
  1885. /// </summary>
  1886. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1887. /// <param name="collection">The collection.</param>
  1888. /// <param name="filter">The filter.</param>
  1889. /// <param name="update">The update.</param>
  1890. /// <param name="options">The options.</param>
  1891. /// <param name="cancellationToken">The cancellation token.</param>
  1892. /// <returns>
  1893. /// The result of the update operation.
  1894. /// </returns>
  1895. public static Task<UpdateResult> UpdateOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1896. {
  1897. Ensure.IsNotNull(collection, nameof(collection));
  1898. Ensure.IsNotNull(filter, nameof(filter));
  1899. return collection.UpdateOneAsync(
  1900. new ExpressionFilterDefinition<TDocument>(filter),
  1901. update,
  1902. options,
  1903. cancellationToken);
  1904. }
  1905. /// <summary>
  1906. /// Updates a single document.
  1907. /// </summary>
  1908. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1909. /// <param name="collection">The collection.</param>
  1910. /// <param name="session">The session.</param>
  1911. /// <param name="filter">The filter.</param>
  1912. /// <param name="update">The update.</param>
  1913. /// <param name="options">The options.</param>
  1914. /// <param name="cancellationToken">The cancellation token.</param>
  1915. /// <returns>
  1916. /// The result of the update operation.
  1917. /// </returns>
  1918. public static Task<UpdateResult> UpdateOneAsync<TDocument>(this IMongoCollection<TDocument> collection, IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  1919. {
  1920. Ensure.IsNotNull(collection, nameof(collection));
  1921. Ensure.IsNotNull(session, nameof(session));
  1922. Ensure.IsNotNull(filter, nameof(filter));
  1923. return collection.UpdateOneAsync(
  1924. session,
  1925. new ExpressionFilterDefinition<TDocument>(filter),
  1926. update,
  1927. options,
  1928. cancellationToken);
  1929. }
  1930. /// <summary>
  1931. /// Watches changes on the collection.
  1932. /// </summary>
  1933. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1934. /// <param name="collection">The collection.</param>
  1935. /// <param name="options">The options.</param>
  1936. /// <param name="cancellationToken">The cancellation token.</param>
  1937. /// <returns>
  1938. /// A change stream.
  1939. /// </returns>
  1940. public static IAsyncCursor<ChangeStreamDocument<TDocument>> Watch<TDocument>(
  1941. this IMongoCollection<TDocument> collection,
  1942. ChangeStreamOptions options = null,
  1943. CancellationToken cancellationToken = default(CancellationToken))
  1944. {
  1945. Ensure.IsNotNull(collection, nameof(collection));
  1946. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<TDocument>>();
  1947. return collection.Watch(emptyPipeline, options, cancellationToken);
  1948. }
  1949. /// <summary>
  1950. /// Watches changes on the collection.
  1951. /// </summary>
  1952. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1953. /// <param name="collection">The collection.</param>
  1954. /// <param name="session">The session.</param>
  1955. /// <param name="options">The options.</param>
  1956. /// <param name="cancellationToken">The cancellation token.</param>
  1957. /// <returns>
  1958. /// A change stream.
  1959. /// </returns>
  1960. public static IAsyncCursor<ChangeStreamDocument<TDocument>> Watch<TDocument>(
  1961. this IMongoCollection<TDocument> collection,
  1962. IClientSessionHandle session,
  1963. ChangeStreamOptions options = null,
  1964. CancellationToken cancellationToken = default(CancellationToken))
  1965. {
  1966. Ensure.IsNotNull(collection, nameof(collection));
  1967. Ensure.IsNotNull(session, nameof(session));
  1968. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<TDocument>>();
  1969. return collection.Watch(session, emptyPipeline, options, cancellationToken);
  1970. }
  1971. /// <summary>
  1972. /// Watches changes on the collection.
  1973. /// </summary>
  1974. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1975. /// <param name="collection">The collection.</param>
  1976. /// <param name="options">The options.</param>
  1977. /// <param name="cancellationToken">The cancellation token.</param>
  1978. /// <returns>
  1979. /// A change stream.
  1980. /// </returns>
  1981. public static Task<IAsyncCursor<ChangeStreamDocument<TDocument>>> WatchAsync<TDocument>(
  1982. this IMongoCollection<TDocument> collection,
  1983. ChangeStreamOptions options = null,
  1984. CancellationToken cancellationToken = default(CancellationToken))
  1985. {
  1986. Ensure.IsNotNull(collection, nameof(collection));
  1987. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<TDocument>>();
  1988. return collection.WatchAsync(emptyPipeline, options, cancellationToken);
  1989. }
  1990. /// <summary>
  1991. /// Watches changes on the collection.
  1992. /// </summary>
  1993. /// <typeparam name="TDocument">The type of the document.</typeparam>
  1994. /// <param name="collection">The collection.</param>
  1995. /// <param name="session">The session.</param>
  1996. /// <param name="options">The options.</param>
  1997. /// <param name="cancellationToken">The cancellation token.</param>
  1998. /// <returns>
  1999. /// A change stream.
  2000. /// </returns>
  2001. public static Task<IAsyncCursor<ChangeStreamDocument<TDocument>>> WatchAsync<TDocument>(
  2002. this IMongoCollection<TDocument> collection,
  2003. IClientSessionHandle session,
  2004. ChangeStreamOptions options = null,
  2005. CancellationToken cancellationToken = default(CancellationToken))
  2006. {
  2007. Ensure.IsNotNull(collection, nameof(collection));
  2008. Ensure.IsNotNull(session, nameof(session));
  2009. var emptyPipeline = new EmptyPipelineDefinition<ChangeStreamDocument<TDocument>>();
  2010. return collection.WatchAsync(session, emptyPipeline, options, cancellationToken);
  2011. }
  2012. }
  2013. }