IMongoCollectionExtensions.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /* Copyright 2010-2016 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>(collection, emptyPipeline, options ?? new AggregateOptions());
  42. }
  43. /// <summary>
  44. /// Creates a queryable source of documents.
  45. /// </summary>
  46. /// <typeparam name="TDocument">The type of the document.</typeparam>
  47. /// <param name="collection">The collection.</param>
  48. /// <param name="aggregateOptions">The aggregate options</param>
  49. /// <returns>A queryable source of documents.</returns>
  50. public static IMongoQueryable<TDocument> AsQueryable<TDocument>(this IMongoCollection<TDocument> collection, AggregateOptions aggregateOptions = null)
  51. {
  52. Ensure.IsNotNull(collection, nameof(collection));
  53. aggregateOptions = aggregateOptions ?? new AggregateOptions();
  54. var provider = new MongoQueryProviderImpl<TDocument>(collection, aggregateOptions);
  55. return new MongoQueryableImpl<TDocument, TDocument>(provider);
  56. }
  57. /// <summary>
  58. /// Counts the number of documents in the collection.
  59. /// </summary>
  60. /// <typeparam name="TDocument">The type of the document.</typeparam>
  61. /// <param name="collection">The collection.</param>
  62. /// <param name="filter">The filter.</param>
  63. /// <param name="options">The options.</param>
  64. /// <param name="cancellationToken">The cancellation token.</param>
  65. /// <returns>
  66. /// The number of documents in the collection.
  67. /// </returns>
  68. public static long Count<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CountOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  69. {
  70. Ensure.IsNotNull(collection, nameof(collection));
  71. Ensure.IsNotNull(filter, nameof(filter));
  72. return collection.Count(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  73. }
  74. /// <summary>
  75. /// Counts the number of documents in the collection.
  76. /// </summary>
  77. /// <typeparam name="TDocument">The type of the document.</typeparam>
  78. /// <param name="collection">The collection.</param>
  79. /// <param name="filter">The filter.</param>
  80. /// <param name="options">The options.</param>
  81. /// <param name="cancellationToken">The cancellation token.</param>
  82. /// <returns>
  83. /// The number of documents in the collection.
  84. /// </returns>
  85. public static Task<long> CountAsync<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.CountAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  90. }
  91. /// <summary>
  92. /// Deletes multiple documents.
  93. /// </summary>
  94. /// <typeparam name="TDocument">The type of the document.</typeparam>
  95. /// <param name="collection">The collection.</param>
  96. /// <param name="filter">The filter.</param>
  97. /// <param name="cancellationToken">The cancellation token.</param>
  98. /// <returns>
  99. /// The result of the delete operation.
  100. /// </returns>
  101. public static DeleteResult DeleteMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  102. {
  103. return collection.DeleteMany<TDocument>(filter, null, cancellationToken);
  104. }
  105. /// <summary>
  106. /// Deletes multiple documents.
  107. /// </summary>
  108. /// <typeparam name="TDocument">The type of the document.</typeparam>
  109. /// <param name="collection">The collection.</param>
  110. /// <param name="filter">The filter.</param>
  111. /// <param name="options">The options.</param>
  112. /// <param name="cancellationToken">The cancellation token.</param>
  113. /// <returns>
  114. /// The result of the delete operation.
  115. /// </returns>
  116. public static DeleteResult DeleteMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  117. {
  118. Ensure.IsNotNull(collection, nameof(collection));
  119. Ensure.IsNotNull(filter, nameof(filter));
  120. return collection.DeleteMany(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  121. }
  122. /// <summary>
  123. /// Deletes multiple documents.
  124. /// </summary>
  125. /// <typeparam name="TDocument">The type of the document.</typeparam>
  126. /// <param name="collection">The collection.</param>
  127. /// <param name="filter">The filter.</param>
  128. /// <param name="cancellationToken">The cancellation token.</param>
  129. /// <returns>
  130. /// The result of the delete operation.
  131. /// </returns>
  132. public static Task<DeleteResult> DeleteManyAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  133. {
  134. return collection.DeleteManyAsync<TDocument>(filter, null, cancellationToken);
  135. }
  136. /// <summary>
  137. /// Deletes multiple documents.
  138. /// </summary>
  139. /// <typeparam name="TDocument">The type of the document.</typeparam>
  140. /// <param name="collection">The collection.</param>
  141. /// <param name="filter">The filter.</param>
  142. /// <param name="options">The options.</param>
  143. /// <param name="cancellationToken">The cancellation token.</param>
  144. /// <returns>
  145. /// The result of the delete operation.
  146. /// </returns>
  147. public static Task<DeleteResult> DeleteManyAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  148. {
  149. Ensure.IsNotNull(collection, nameof(collection));
  150. Ensure.IsNotNull(filter, nameof(filter));
  151. return collection.DeleteManyAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  152. }
  153. /// <summary>
  154. /// Deletes a single document.
  155. /// </summary>
  156. /// <typeparam name="TDocument">The type of the document.</typeparam>
  157. /// <param name="collection">The collection.</param>
  158. /// <param name="filter">The filter.</param>
  159. /// <param name="cancellationToken">The cancellation token.</param>
  160. /// <returns>
  161. /// The result of the delete operation.
  162. /// </returns>
  163. public static DeleteResult DeleteOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  164. {
  165. return collection.DeleteOne<TDocument>(filter, null, cancellationToken);
  166. }
  167. /// <summary>
  168. /// Deletes a single document.
  169. /// </summary>
  170. /// <typeparam name="TDocument">The type of the document.</typeparam>
  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 result of the delete operation.
  177. /// </returns>
  178. public static DeleteResult DeleteOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  179. {
  180. Ensure.IsNotNull(collection, nameof(collection));
  181. Ensure.IsNotNull(filter, nameof(filter));
  182. return collection.DeleteOne(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  183. }
  184. /// <summary>
  185. /// Deletes a single document.
  186. /// </summary>
  187. /// <typeparam name="TDocument">The type of the document.</typeparam>
  188. /// <param name="collection">The collection.</param>
  189. /// <param name="filter">The filter.</param>
  190. /// <param name="cancellationToken">The cancellation token.</param>
  191. /// <returns>
  192. /// The result of the delete operation.
  193. /// </returns>
  194. public static Task<DeleteResult> DeleteOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken = default(CancellationToken))
  195. {
  196. return collection.DeleteOneAsync<TDocument>(filter, null, cancellationToken);
  197. }
  198. /// <summary>
  199. /// Deletes a single document.
  200. /// </summary>
  201. /// <typeparam name="TDocument">The type of the document.</typeparam>
  202. /// <param name="collection">The collection.</param>
  203. /// <param name="filter">The filter.</param>
  204. /// <param name="options">The options.</param>
  205. /// <param name="cancellationToken">The cancellation token.</param>
  206. /// <returns>
  207. /// The result of the delete operation.
  208. /// </returns>
  209. public static Task<DeleteResult> DeleteOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, DeleteOptions options, CancellationToken cancellationToken = default(CancellationToken))
  210. {
  211. Ensure.IsNotNull(collection, nameof(collection));
  212. Ensure.IsNotNull(filter, nameof(filter));
  213. return collection.DeleteOneAsync(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  214. }
  215. /// <summary>
  216. /// Gets the distinct values for a specified field.
  217. /// </summary>
  218. /// <typeparam name="TDocument">The type of the document.</typeparam>
  219. /// <typeparam name="TField">The type of the result.</typeparam>
  220. /// <param name="collection">The collection.</param>
  221. /// <param name="field">The field.</param>
  222. /// <param name="filter">The filter.</param>
  223. /// <param name="options">The options.</param>
  224. /// <param name="cancellationToken">The cancellation token.</param>
  225. /// <returns>
  226. /// The distinct values for the specified field.
  227. /// </returns>
  228. 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))
  229. {
  230. Ensure.IsNotNull(collection, nameof(collection));
  231. Ensure.IsNotNull(field, nameof(field));
  232. Ensure.IsNotNull(filter, nameof(filter));
  233. return collection.Distinct<TField>(
  234. new ExpressionFieldDefinition<TDocument, TField>(field),
  235. filter,
  236. options,
  237. cancellationToken);
  238. }
  239. /// <summary>
  240. /// Gets the distinct values for a specified field.
  241. /// </summary>
  242. /// <typeparam name="TDocument">The type of the document.</typeparam>
  243. /// <typeparam name="TField">The type of the result.</typeparam>
  244. /// <param name="collection">The collection.</param>
  245. /// <param name="field">The field.</param>
  246. /// <param name="filter">The filter.</param>
  247. /// <param name="options">The options.</param>
  248. /// <param name="cancellationToken">The cancellation token.</param>
  249. /// <returns>
  250. /// The distinct values for the specified field.
  251. /// </returns>
  252. 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))
  253. {
  254. Ensure.IsNotNull(collection, nameof(collection));
  255. Ensure.IsNotNull(field, nameof(field));
  256. Ensure.IsNotNull(filter, nameof(filter));
  257. return collection.Distinct<TField>(
  258. field,
  259. new ExpressionFilterDefinition<TDocument>(filter),
  260. options,
  261. cancellationToken);
  262. }
  263. /// <summary>
  264. /// Gets the distinct values for a specified field.
  265. /// </summary>
  266. /// <typeparam name="TDocument">The type of the document.</typeparam>
  267. /// <typeparam name="TField">The type of the result.</typeparam>
  268. /// <param name="collection">The collection.</param>
  269. /// <param name="field">The field.</param>
  270. /// <param name="filter">The filter.</param>
  271. /// <param name="options">The options.</param>
  272. /// <param name="cancellationToken">The cancellation token.</param>
  273. /// <returns>
  274. /// The distinct values for the specified field.
  275. /// </returns>
  276. 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))
  277. {
  278. Ensure.IsNotNull(collection, nameof(collection));
  279. Ensure.IsNotNull(field, nameof(field));
  280. Ensure.IsNotNull(filter, nameof(filter));
  281. return collection.Distinct<TField>(
  282. new ExpressionFieldDefinition<TDocument, TField>(field),
  283. new ExpressionFilterDefinition<TDocument>(filter),
  284. options,
  285. cancellationToken);
  286. }
  287. /// <summary>
  288. /// Gets the distinct values for a specified field.
  289. /// </summary>
  290. /// <typeparam name="TDocument">The type of the document.</typeparam>
  291. /// <typeparam name="TField">The type of the result.</typeparam>
  292. /// <param name="collection">The collection.</param>
  293. /// <param name="field">The field.</param>
  294. /// <param name="filter">The filter.</param>
  295. /// <param name="options">The options.</param>
  296. /// <param name="cancellationToken">The cancellation token.</param>
  297. /// <returns>
  298. /// The distinct values for the specified field.
  299. /// </returns>
  300. 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))
  301. {
  302. Ensure.IsNotNull(collection, nameof(collection));
  303. Ensure.IsNotNull(field, nameof(field));
  304. Ensure.IsNotNull(filter, nameof(filter));
  305. return collection.DistinctAsync<TField>(
  306. new ExpressionFieldDefinition<TDocument, TField>(field),
  307. filter,
  308. options,
  309. cancellationToken);
  310. }
  311. /// <summary>
  312. /// Gets the distinct values for a specified field.
  313. /// </summary>
  314. /// <typeparam name="TDocument">The type of the document.</typeparam>
  315. /// <typeparam name="TField">The type of the result.</typeparam>
  316. /// <param name="collection">The collection.</param>
  317. /// <param name="field">The field.</param>
  318. /// <param name="filter">The filter.</param>
  319. /// <param name="options">The options.</param>
  320. /// <param name="cancellationToken">The cancellation token.</param>
  321. /// <returns>
  322. /// The distinct values for the specified field.
  323. /// </returns>
  324. 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))
  325. {
  326. Ensure.IsNotNull(collection, nameof(collection));
  327. Ensure.IsNotNull(field, nameof(field));
  328. Ensure.IsNotNull(filter, nameof(filter));
  329. return collection.DistinctAsync<TField>(
  330. field,
  331. new ExpressionFilterDefinition<TDocument>(filter),
  332. options,
  333. cancellationToken);
  334. }
  335. /// <summary>
  336. /// Gets the distinct values for a specified field.
  337. /// </summary>
  338. /// <typeparam name="TDocument">The type of the document.</typeparam>
  339. /// <typeparam name="TField">The type of the result.</typeparam>
  340. /// <param name="collection">The collection.</param>
  341. /// <param name="field">The field.</param>
  342. /// <param name="filter">The filter.</param>
  343. /// <param name="options">The options.</param>
  344. /// <param name="cancellationToken">The cancellation token.</param>
  345. /// <returns>
  346. /// The distinct values for the specified field.
  347. /// </returns>
  348. 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))
  349. {
  350. Ensure.IsNotNull(collection, nameof(collection));
  351. Ensure.IsNotNull(field, nameof(field));
  352. Ensure.IsNotNull(filter, nameof(filter));
  353. return collection.DistinctAsync<TField>(
  354. new ExpressionFieldDefinition<TDocument, TField>(field),
  355. new ExpressionFilterDefinition<TDocument>(filter),
  356. options,
  357. cancellationToken);
  358. }
  359. /// <summary>
  360. /// Begins a fluent find interface.
  361. /// </summary>
  362. /// <typeparam name="TDocument">The type of the document.</typeparam>
  363. /// <param name="collection">The collection.</param>
  364. /// <param name="filter">The filter.</param>
  365. /// <param name="options">The options.</param>
  366. /// <returns>
  367. /// A fluent find interface.
  368. /// </returns>
  369. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions options = null)
  370. {
  371. FindOptions<TDocument, TDocument> genericOptions;
  372. if (options == null)
  373. {
  374. genericOptions = new FindOptions<TDocument>();
  375. }
  376. else
  377. {
  378. genericOptions = new FindOptions<TDocument>
  379. {
  380. AllowPartialResults = options.AllowPartialResults,
  381. BatchSize = options.BatchSize,
  382. Collation = options.Collation,
  383. Comment = options.Comment,
  384. CursorType = options.CursorType,
  385. MaxAwaitTime = options.MaxAwaitTime,
  386. MaxTime = options.MaxTime,
  387. Modifiers = options.Modifiers,
  388. NoCursorTimeout = options.NoCursorTimeout,
  389. OplogReplay = options.OplogReplay
  390. };
  391. }
  392. return new FindFluent<TDocument, TDocument>(collection, filter, genericOptions);
  393. }
  394. /// <summary>
  395. /// Begins a fluent find interface.
  396. /// </summary>
  397. /// <typeparam name="TDocument">The type of the document.</typeparam>
  398. /// <param name="collection">The collection.</param>
  399. /// <param name="filter">The filter.</param>
  400. /// <param name="options">The options.</param>
  401. /// <returns>
  402. /// A fluent interface.
  403. /// </returns>
  404. public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions options = null)
  405. {
  406. Ensure.IsNotNull(collection, nameof(collection));
  407. Ensure.IsNotNull(filter, nameof(filter));
  408. return collection.Find(new ExpressionFilterDefinition<TDocument>(filter), options);
  409. }
  410. /// <summary>
  411. /// Finds the documents matching the filter.
  412. /// </summary>
  413. /// <typeparam name="TDocument">The type of the document.</typeparam>
  414. /// <param name="collection">The collection.</param>
  415. /// <param name="filter">The filter.</param>
  416. /// <param name="options">The options.</param>
  417. /// <param name="cancellationToken">The cancellation token.</param>
  418. /// <returns>A Task whose result is a cursor.</returns>
  419. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  420. {
  421. Ensure.IsNotNull(collection, nameof(collection));
  422. Ensure.IsNotNull(filter, nameof(filter));
  423. return collection.FindSync<TDocument>(filter, options, cancellationToken);
  424. }
  425. /// <summary>
  426. /// Finds the documents matching the filter.
  427. /// </summary>
  428. /// <typeparam name="TDocument">The type of the document.</typeparam>
  429. /// <param name="collection">The collection.</param>
  430. /// <param name="filter">The filter.</param>
  431. /// <param name="options">The options.</param>
  432. /// <param name="cancellationToken">The cancellation token.</param>
  433. /// <returns>A Task whose result is a cursor.</returns>
  434. public static IAsyncCursor<TDocument> FindSync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  435. {
  436. Ensure.IsNotNull(collection, nameof(collection));
  437. Ensure.IsNotNull(filter, nameof(filter));
  438. return collection.FindSync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  439. }
  440. /// <summary>
  441. /// Finds the documents matching the filter.
  442. /// </summary>
  443. /// <typeparam name="TDocument">The type of the document.</typeparam>
  444. /// <param name="collection">The collection.</param>
  445. /// <param name="filter">The filter.</param>
  446. /// <param name="options">The options.</param>
  447. /// <param name="cancellationToken">The cancellation token.</param>
  448. /// <returns>A Task whose result is a cursor.</returns>
  449. public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  450. {
  451. Ensure.IsNotNull(collection, nameof(collection));
  452. Ensure.IsNotNull(filter, nameof(filter));
  453. return collection.FindAsync<TDocument>(filter, options, cancellationToken);
  454. }
  455. /// <summary>
  456. /// Finds the documents matching the filter.
  457. /// </summary>
  458. /// <typeparam name="TDocument">The type of the document.</typeparam>
  459. /// <param name="collection">The collection.</param>
  460. /// <param name="filter">The filter.</param>
  461. /// <param name="options">The options.</param>
  462. /// <param name="cancellationToken">The cancellation token.</param>
  463. /// <returns>A Task whose result is a cursor.</returns>
  464. 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))
  465. {
  466. Ensure.IsNotNull(collection, nameof(collection));
  467. Ensure.IsNotNull(filter, nameof(filter));
  468. return collection.FindAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  469. }
  470. /// <summary>
  471. /// Finds a single document and deletes it atomically.
  472. /// </summary>
  473. /// <typeparam name="TDocument">The type of the document.</typeparam>
  474. /// <param name="collection">The collection.</param>
  475. /// <param name="filter">The filter.</param>
  476. /// <param name="options">The options.</param>
  477. /// <param name="cancellationToken">The cancellation token.</param>
  478. /// <returns>
  479. /// The deleted document if one was deleted.
  480. /// </returns>
  481. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  482. {
  483. Ensure.IsNotNull(collection, nameof(collection));
  484. Ensure.IsNotNull(filter, nameof(filter));
  485. return collection.FindOneAndDelete<TDocument>(filter, options, cancellationToken);
  486. }
  487. /// <summary>
  488. /// Finds a single document and deletes it atomically.
  489. /// </summary>
  490. /// <typeparam name="TDocument">The type of the document.</typeparam>
  491. /// <param name="collection">The collection.</param>
  492. /// <param name="filter">The filter.</param>
  493. /// <param name="options">The options.</param>
  494. /// <param name="cancellationToken">The cancellation token.</param>
  495. /// <returns>
  496. /// The deleted document if one was deleted.
  497. /// </returns>
  498. public static TDocument FindOneAndDelete<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  499. {
  500. Ensure.IsNotNull(collection, nameof(collection));
  501. Ensure.IsNotNull(filter, nameof(filter));
  502. return collection.FindOneAndDelete<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  503. }
  504. /// <summary>
  505. /// Finds a single document and deletes it atomically.
  506. /// </summary>
  507. /// <typeparam name="TDocument">The type of the document.</typeparam>
  508. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  509. /// <param name="collection">The collection.</param>
  510. /// <param name="filter">The filter.</param>
  511. /// <param name="options">The options.</param>
  512. /// <param name="cancellationToken">The cancellation token.</param>
  513. /// <returns>
  514. /// The returned document.
  515. /// </returns>
  516. public static TProjection FindOneAndDelete<TDocument, TProjection>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TProjection> options = null, CancellationToken cancellationToken = default(CancellationToken))
  517. {
  518. Ensure.IsNotNull(collection, nameof(collection));
  519. Ensure.IsNotNull(filter, nameof(filter));
  520. return collection.FindOneAndDelete<TProjection>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  521. }
  522. /// <summary>
  523. /// Finds a single document and deletes it atomically.
  524. /// </summary>
  525. /// <typeparam name="TDocument">The type of the document.</typeparam>
  526. /// <param name="collection">The collection.</param>
  527. /// <param name="filter">The filter.</param>
  528. /// <param name="options">The options.</param>
  529. /// <param name="cancellationToken">The cancellation token.</param>
  530. /// <returns>
  531. /// The deleted document if one was deleted.
  532. /// </returns>
  533. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  534. {
  535. Ensure.IsNotNull(collection, nameof(collection));
  536. Ensure.IsNotNull(filter, nameof(filter));
  537. return collection.FindOneAndDeleteAsync<TDocument>(filter, options, cancellationToken);
  538. }
  539. /// <summary>
  540. /// Finds a single document and deletes it atomically.
  541. /// </summary>
  542. /// <typeparam name="TDocument">The type of the document.</typeparam>
  543. /// <param name="collection">The collection.</param>
  544. /// <param name="filter">The filter.</param>
  545. /// <param name="options">The options.</param>
  546. /// <param name="cancellationToken">The cancellation token.</param>
  547. /// <returns>
  548. /// The deleted document if one was deleted.
  549. /// </returns>
  550. public static Task<TDocument> FindOneAndDeleteAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, FindOneAndDeleteOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  551. {
  552. Ensure.IsNotNull(collection, nameof(collection));
  553. Ensure.IsNotNull(filter, nameof(filter));
  554. return collection.FindOneAndDeleteAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  555. }
  556. /// <summary>
  557. /// Finds a single document and deletes it atomically.
  558. /// </summary>
  559. /// <typeparam name="TDocument">The type of the document.</typeparam>
  560. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  561. /// <param name="collection">The collection.</param>
  562. /// <param name="filter">The filter.</param>
  563. /// <param name="options">The options.</param>
  564. /// <param name="cancellationToken">The cancellation token.</param>
  565. /// <returns>
  566. /// The returned document.
  567. /// </returns>
  568. 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))
  569. {
  570. Ensure.IsNotNull(collection, nameof(collection));
  571. Ensure.IsNotNull(filter, nameof(filter));
  572. return collection.FindOneAndDeleteAsync<TProjection>(new ExpressionFilterDefinition<TDocument>(filter), options, cancellationToken);
  573. }
  574. /// <summary>
  575. /// Finds a single document and replaces it atomically.
  576. /// </summary>
  577. /// <typeparam name="TDocument">The type of the document.</typeparam>
  578. /// <param name="collection">The collection.</param>
  579. /// <param name="filter">The filter.</param>
  580. /// <param name="replacement">The replacement.</param>
  581. /// <param name="options">The options.</param>
  582. /// <param name="cancellationToken">The cancellation token.</param>
  583. /// <returns>
  584. /// The returned document.
  585. /// </returns>
  586. public static TDocument FindOneAndReplace<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  587. {
  588. Ensure.IsNotNull(collection, nameof(collection));
  589. Ensure.IsNotNull(filter, nameof(filter));
  590. return collection.FindOneAndReplace<TDocument>(filter, replacement, options, cancellationToken);
  591. }
  592. /// <summary>
  593. /// Finds a single document and replaces it atomically.
  594. /// </summary>
  595. /// <typeparam name="TDocument">The type of the document.</typeparam>
  596. /// <param name="collection">The collection.</param>
  597. /// <param name="filter">The filter.</param>
  598. /// <param name="replacement">The replacement.</param>
  599. /// <param name="options">The options.</param>
  600. /// <param name="cancellationToken">The cancellation token.</param>
  601. /// <returns>
  602. /// The returned document.
  603. /// </returns>
  604. 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))
  605. {
  606. Ensure.IsNotNull(collection, nameof(collection));
  607. Ensure.IsNotNull(filter, nameof(filter));
  608. return collection.FindOneAndReplace<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  609. }
  610. /// <summary>
  611. /// Finds a single document and replaces it atomically.
  612. /// </summary>
  613. /// <typeparam name="TDocument">The type of the document.</typeparam>
  614. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  615. /// <param name="collection">The collection.</param>
  616. /// <param name="filter">The filter.</param>
  617. /// <param name="replacement">The replacement.</param>
  618. /// <param name="options">The options.</param>
  619. /// <param name="cancellationToken">The cancellation token.</param>
  620. /// <returns>
  621. /// The returned document.
  622. /// </returns>
  623. 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))
  624. {
  625. Ensure.IsNotNull(collection, nameof(collection));
  626. Ensure.IsNotNull(filter, nameof(filter));
  627. return collection.FindOneAndReplace<TProjection>(filter, replacement, options, cancellationToken);
  628. }
  629. /// <summary>
  630. /// Finds a single document and replaces it atomically.
  631. /// </summary>
  632. /// <typeparam name="TDocument">The type of the document.</typeparam>
  633. /// <param name="collection">The collection.</param>
  634. /// <param name="filter">The filter.</param>
  635. /// <param name="replacement">The replacement.</param>
  636. /// <param name="options">The options.</param>
  637. /// <param name="cancellationToken">The cancellation token.</param>
  638. /// <returns>
  639. /// The returned document.
  640. /// </returns>
  641. public static Task<TDocument> FindOneAndReplaceAsync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, TDocument replacement, FindOneAndReplaceOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  642. {
  643. Ensure.IsNotNull(collection, nameof(collection));
  644. Ensure.IsNotNull(filter, nameof(filter));
  645. return collection.FindOneAndReplaceAsync<TDocument>(filter, replacement, options, cancellationToken);
  646. }
  647. /// <summary>
  648. /// Finds a single document and replaces it atomically.
  649. /// </summary>
  650. /// <typeparam name="TDocument">The type of the document.</typeparam>
  651. /// <param name="collection">The collection.</param>
  652. /// <param name="filter">The filter.</param>
  653. /// <param name="replacement">The replacement.</param>
  654. /// <param name="options">The options.</param>
  655. /// <param name="cancellationToken">The cancellation token.</param>
  656. /// <returns>
  657. /// The returned document.
  658. /// </returns>
  659. 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))
  660. {
  661. Ensure.IsNotNull(collection, nameof(collection));
  662. Ensure.IsNotNull(filter, nameof(filter));
  663. return collection.FindOneAndReplaceAsync<TDocument>(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  664. }
  665. /// <summary>
  666. /// Finds a single document and replaces it atomically.
  667. /// </summary>
  668. /// <typeparam name="TDocument">The type of the document.</typeparam>
  669. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  670. /// <param name="collection">The collection.</param>
  671. /// <param name="filter">The filter.</param>
  672. /// <param name="replacement">The replacement.</param>
  673. /// <param name="options">The options.</param>
  674. /// <param name="cancellationToken">The cancellation token.</param>
  675. /// <returns>
  676. /// The returned document.
  677. /// </returns>
  678. 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))
  679. {
  680. Ensure.IsNotNull(collection, nameof(collection));
  681. Ensure.IsNotNull(filter, nameof(filter));
  682. return collection.FindOneAndReplaceAsync<TProjection>(filter, replacement, options, cancellationToken);
  683. }
  684. /// <summary>
  685. /// Finds a single document and updates it atomically.
  686. /// </summary>
  687. /// <typeparam name="TDocument">The type of the document.</typeparam>
  688. /// <param name="collection">The collection.</param>
  689. /// <param name="filter">The filter.</param>
  690. /// <param name="update">The update.</param>
  691. /// <param name="options">The options.</param>
  692. /// <param name="cancellationToken">The cancellation token.</param>
  693. /// <returns>
  694. /// The returned document.
  695. /// </returns>
  696. public static TDocument FindOneAndUpdate<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options = null, CancellationToken cancellationToken = default(CancellationToken))
  697. {
  698. Ensure.IsNotNull(collection, nameof(collection));
  699. Ensure.IsNotNull(filter, nameof(filter));
  700. Ensure.IsNotNull(update, nameof(update));
  701. return collection.FindOneAndUpdate<TDocument>(
  702. filter,
  703. update,
  704. options,
  705. cancellationToken);
  706. }
  707. /// <summary>
  708. /// Finds a single document and updates it atomically.
  709. /// </summary>
  710. /// <typeparam name="TDocument">The type of the document.</typeparam>
  711. /// <param name="collection">The collection.</param>
  712. /// <param name="filter">The filter.</param>
  713. /// <param name="update">The update.</param>
  714. /// <param name="options">The options.</param>
  715. /// <param name="cancellationToken">The cancellation token.</param>
  716. /// <returns>
  717. /// The returned document.
  718. /// </returns>
  719. 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))
  720. {
  721. Ensure.IsNotNull(collection, nameof(collection));
  722. Ensure.IsNotNull(filter, nameof(filter));
  723. Ensure.IsNotNull(update, nameof(update));
  724. return collection.FindOneAndUpdate<TDocument>(
  725. new ExpressionFilterDefinition<TDocument>(filter),
  726. update,
  727. options,
  728. cancellationToken);
  729. }
  730. /// <summary>
  731. /// Finds a single document and updates it atomically.
  732. /// </summary>
  733. /// <typeparam name="TDocument">The type of the document.</typeparam>
  734. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  735. /// <param name="collection">The collection.</param>
  736. /// <param name="filter">The filter.</param>
  737. /// <param name="update">The update.</param>
  738. /// <param name="options">The options.</param>
  739. /// <param name="cancellationToken">The cancellation token.</param>
  740. /// <returns>
  741. /// The returned document.
  742. /// </returns>
  743. 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))
  744. {
  745. Ensure.IsNotNull(collection, nameof(collection));
  746. Ensure.IsNotNull(filter, nameof(filter));
  747. Ensure.IsNotNull(update, nameof(update));
  748. return collection.FindOneAndUpdate(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  749. }
  750. /// <summary>
  751. /// Finds a single document and updates it atomically.
  752. /// </summary>
  753. /// <typeparam name="TDocument">The type of the document.</typeparam>
  754. /// <param name="collection">The collection.</param>
  755. /// <param name="filter">The filter.</param>
  756. /// <param name="update">The update.</param>
  757. /// <param name="options">The options.</param>
  758. /// <param name="cancellationToken">The cancellation token.</param>
  759. /// <returns>
  760. /// The returned document.
  761. /// </returns>
  762. 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))
  763. {
  764. Ensure.IsNotNull(collection, nameof(collection));
  765. Ensure.IsNotNull(filter, nameof(filter));
  766. Ensure.IsNotNull(update, nameof(update));
  767. return collection.FindOneAndUpdateAsync<TDocument>(
  768. filter,
  769. update,
  770. options,
  771. cancellationToken);
  772. }
  773. /// <summary>
  774. /// Finds a single document and updates it atomically.
  775. /// </summary>
  776. /// <typeparam name="TDocument">The type of the document.</typeparam>
  777. /// <param name="collection">The collection.</param>
  778. /// <param name="filter">The filter.</param>
  779. /// <param name="update">The update.</param>
  780. /// <param name="options">The options.</param>
  781. /// <param name="cancellationToken">The cancellation token.</param>
  782. /// <returns>
  783. /// The returned document.
  784. /// </returns>
  785. 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))
  786. {
  787. Ensure.IsNotNull(collection, nameof(collection));
  788. Ensure.IsNotNull(filter, nameof(filter));
  789. Ensure.IsNotNull(update, nameof(update));
  790. return collection.FindOneAndUpdateAsync<TDocument>(
  791. new ExpressionFilterDefinition<TDocument>(filter),
  792. update,
  793. options,
  794. cancellationToken);
  795. }
  796. /// <summary>
  797. /// Finds a single document and updates it atomically.
  798. /// </summary>
  799. /// <typeparam name="TDocument">The type of the document.</typeparam>
  800. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  801. /// <param name="collection">The collection.</param>
  802. /// <param name="filter">The filter.</param>
  803. /// <param name="update">The update.</param>
  804. /// <param name="options">The options.</param>
  805. /// <param name="cancellationToken">The cancellation token.</param>
  806. /// <returns>
  807. /// The returned document.
  808. /// </returns>
  809. 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))
  810. {
  811. Ensure.IsNotNull(collection, nameof(collection));
  812. Ensure.IsNotNull(filter, nameof(filter));
  813. Ensure.IsNotNull(update, nameof(update));
  814. return collection.FindOneAndUpdateAsync(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  815. }
  816. /// <summary>
  817. /// Replaces a single document.
  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="replacement">The replacement.</param>
  823. /// <param name="options">The options.</param>
  824. /// <param name="cancellationToken">The cancellation token.</param>
  825. /// <returns>
  826. /// The result of the replacement.
  827. /// </returns>
  828. public static ReplaceOneResult ReplaceOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  829. {
  830. Ensure.IsNotNull(collection, nameof(collection));
  831. Ensure.IsNotNull(filter, nameof(filter));
  832. return collection.ReplaceOne(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  833. }
  834. /// <summary>
  835. /// Replaces a single document.
  836. /// </summary>
  837. /// <typeparam name="TDocument">The type of the document.</typeparam>
  838. /// <param name="collection">The collection.</param>
  839. /// <param name="filter">The filter.</param>
  840. /// <param name="replacement">The replacement.</param>
  841. /// <param name="options">The options.</param>
  842. /// <param name="cancellationToken">The cancellation token.</param>
  843. /// <returns>
  844. /// The result of the replacement.
  845. /// </returns>
  846. public static Task<ReplaceOneResult> ReplaceOneAsync<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, TDocument replacement, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  847. {
  848. Ensure.IsNotNull(collection, nameof(collection));
  849. Ensure.IsNotNull(filter, nameof(filter));
  850. return collection.ReplaceOneAsync(new ExpressionFilterDefinition<TDocument>(filter), replacement, options, cancellationToken);
  851. }
  852. /// <summary>
  853. /// Updates many documents.
  854. /// </summary>
  855. /// <typeparam name="TDocument">The type of the document.</typeparam>
  856. /// <param name="collection">The collection.</param>
  857. /// <param name="filter">The filter.</param>
  858. /// <param name="update">The update.</param>
  859. /// <param name="options">The options.</param>
  860. /// <param name="cancellationToken">The cancellation token.</param>
  861. /// <returns>
  862. /// The result of the update operation.
  863. /// </returns>
  864. public static UpdateResult UpdateMany<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  865. {
  866. Ensure.IsNotNull(collection, nameof(collection));
  867. Ensure.IsNotNull(filter, nameof(filter));
  868. return collection.UpdateMany(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  869. }
  870. /// <summary>
  871. /// Updates many documents.
  872. /// </summary>
  873. /// <typeparam name="TDocument">The type of the document.</typeparam>
  874. /// <param name="collection">The collection.</param>
  875. /// <param name="filter">The filter.</param>
  876. /// <param name="update">The update.</param>
  877. /// <param name="options">The options.</param>
  878. /// <param name="cancellationToken">The cancellation token.</param>
  879. /// <returns>
  880. /// The result of the update operation.
  881. /// </returns>
  882. 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))
  883. {
  884. Ensure.IsNotNull(collection, nameof(collection));
  885. Ensure.IsNotNull(filter, nameof(filter));
  886. return collection.UpdateManyAsync(new ExpressionFilterDefinition<TDocument>(filter), update, options, cancellationToken);
  887. }
  888. /// <summary>
  889. /// Updates a single document.
  890. /// </summary>
  891. /// <typeparam name="TDocument">The type of the document.</typeparam>
  892. /// <param name="collection">The collection.</param>
  893. /// <param name="filter">The filter.</param>
  894. /// <param name="update">The update.</param>
  895. /// <param name="options">The options.</param>
  896. /// <param name="cancellationToken">The cancellation token.</param>
  897. /// <returns>
  898. /// The result of the update operation.
  899. /// </returns>
  900. public static UpdateResult UpdateOne<TDocument>(this IMongoCollection<TDocument> collection, Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, UpdateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  901. {
  902. Ensure.IsNotNull(collection, nameof(collection));
  903. Ensure.IsNotNull(filter, nameof(filter));
  904. return collection.UpdateOne(
  905. new ExpressionFilterDefinition<TDocument>(filter),
  906. update,
  907. options,
  908. cancellationToken);
  909. }
  910. /// <summary>
  911. /// Updates a single document.
  912. /// </summary>
  913. /// <typeparam name="TDocument">The type of the document.</typeparam>
  914. /// <param name="collection">The collection.</param>
  915. /// <param name="filter">The filter.</param>
  916. /// <param name="update">The update.</param>
  917. /// <param name="options">The options.</param>
  918. /// <param name="cancellationToken">The cancellation token.</param>
  919. /// <returns>
  920. /// The result of the update operation.
  921. /// </returns>
  922. 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))
  923. {
  924. Ensure.IsNotNull(collection, nameof(collection));
  925. Ensure.IsNotNull(filter, nameof(filter));
  926. return collection.UpdateOneAsync(
  927. new ExpressionFilterDefinition<TDocument>(filter),
  928. update,
  929. options,
  930. cancellationToken);
  931. }
  932. }
  933. }