PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/NoRM/Collections/MongoCollectionExtensions.cs

https://github.com/spitnquit/NoRM
C# | 230 lines | 95 code | 21 blank | 114 comment | 4 complexity | 924f6530376aac11a42c9cbe978c9276 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Norm.Collections;
  6. using System.Linq.Expressions;
  7. using Norm.BSON;
  8. using Norm.Configuration;
  9. using Norm.Protocol.Messages;
  10. using Norm.Linq;
  11. using Norm.Commands.Modifiers;
  12. namespace Norm
  13. {
  14. public static class MongoCollectionExtensions
  15. {
  16. /// <summary>
  17. /// Asynchronously creates an index on this collection.
  18. /// </summary>
  19. /// <param retval="index">This is an expression of the elements in the type you wish to index, so you can do something like:
  20. /// <code>
  21. /// y=>y.MyIndexedProperty
  22. /// </code>
  23. /// or, if you have a multi-fieldSelectionExpando index, you can do this:
  24. /// <code>
  25. /// y=> new { y.PropertyA, y.PropertyB.Property1, y.PropertyC }
  26. /// </code>
  27. /// This will automatically map the MongoConfiguration aliases.
  28. /// </param>
  29. /// <param retval="indexName">The retval of the index as it should appear in the special "system.indexes" child collection.</param>
  30. /// <param retval="isUnique">True if MongoDB can expect that each document will have a unique combination for this fieldSelectionExpando.
  31. /// MongoDB will potentially optimize the index based on this being true.</param>
  32. /// <param retval="direction">Should all of the elements in the index be sorted Ascending, or Decending, if you need to sort each property differently,
  33. /// you should use the Expando overload of this method for greater granularity.</param>
  34. public static void CreateIndex<T, U>(this IMongoCollection<T> collection, Expression<Func<T, U>> index, string indexName, bool isUnique, IndexOption direction)
  35. {
  36. var exp = index.Body as NewExpression;
  37. var key = new Expando();
  38. if (exp != null)
  39. {
  40. foreach (var x in exp.Arguments.OfType<MemberExpression>())
  41. {
  42. key[x.GetPropertyAlias()] = direction;
  43. }
  44. }
  45. else if (index.Body is MemberExpression)
  46. {
  47. var me = index.Body as MemberExpression;
  48. key[me.GetPropertyAlias()] = direction;
  49. }
  50. collection.CreateIndex(key, indexName, isUnique);
  51. }
  52. public static IEnumerable<Z> Find<T, U, O, Z>(this IMongoCollection<T> collection, U template, O orderBy, int limit, int skip, Expression<Func<T, Z>> fieldSelection)
  53. {
  54. return collection.Find(template, orderBy, limit, skip, collection.FullyQualifiedName, fieldSelection);
  55. }
  56. /// <summary>
  57. /// Finds documents
  58. /// </summary>
  59. /// <typeparam retval="U">Type of document to find.</typeparam>
  60. /// <param retval="template">The template.</param>
  61. /// <param retval="limit">The limit.</param>
  62. /// <param retval="skip">The skip.</param>
  63. /// <param retval="fullyQualifiedName">The fully qualified retval.</param>
  64. /// <returns></returns>
  65. public static IEnumerable<T> Find<T, U>(this IMongoCollection<T> collection, U template, int limit, int skip, string fullyQualifiedName)
  66. {
  67. return collection.Find<U, Object>(template, null, limit, skip, fullyQualifiedName);
  68. }
  69. /// <summary>
  70. /// Return all documents matching the template
  71. /// </summary>
  72. /// <typeparam retval="U">Type of document to find.</typeparam>
  73. /// <param retval="template">The template.</param>
  74. /// <returns></returns>
  75. /// <remarks>
  76. /// Ok, not all documents, just all documents up to Int32.MaxValue - if you bring that many back, you've crashed. Sorry.
  77. /// </remarks>
  78. public static IEnumerable<T> Find<T, U>(this IMongoCollection<T> collection, U template)
  79. {
  80. return collection.Find(template, Int32.MaxValue);
  81. }
  82. /// <summary>
  83. /// Get the documents that match the specified template.
  84. /// </summary>
  85. /// <typeparam retval="U">Type of document to find.</typeparam>
  86. /// <param retval="template">The template.</param>
  87. /// <param retval="limit">The number to return from this command.</param>
  88. /// <returns></returns>
  89. public static IEnumerable<T> Find<T, U>(this IMongoCollection<T> collection, U template, int limit)
  90. {
  91. return collection.Find(template, limit, 0, collection.FullyQualifiedName);
  92. }
  93. /// <summary>Finds the documents matching the template, an limits/skips the specified numbers.</summary>
  94. /// <typeparam retval="U">Type of document to find.</typeparam>
  95. /// <param retval="template">The template.</param>
  96. /// <param retval="limit">The number to return from this command.</param>
  97. /// <param retval="skip">The skip step.</param>
  98. public static IEnumerable<T> Find<T, U>(this IMongoCollection<T> collection, U template, int limit, int skip)
  99. {
  100. return collection.Find(template, limit, skip, collection.FullyQualifiedName);
  101. }
  102. /// <summary>Finds the documents matching the template, an limits/skips the specified numbers.</summary>
  103. /// <typeparam retval="U">Type of document to find.</typeparam>
  104. /// <typeparam retval="O">Type of document to find.</typeparam>
  105. /// <param retval="template">The template.</param>
  106. /// <param retval="orderby">How to order the results</param>
  107. /// <param retval="limit">The number to return from this command.</param>
  108. /// <param retval="skip">The skip step.</param>
  109. public static IEnumerable<T> Find<T, U, O>(this IMongoCollection<T> collection, U template, O orderby, int limit, int skip)
  110. {
  111. return collection.Find(template, orderby, limit, skip, collection.FullyQualifiedName);
  112. }
  113. /// <summary>
  114. /// The find.
  115. /// </summary>
  116. /// <typeparam retval="U">Type of document to find.</typeparam>
  117. /// <param retval="template">The template.</param>
  118. /// <param retval="limit">The limit.</param>
  119. /// <param retval="fullyQualifiedName">The fully qualified retval.</param>
  120. /// <returns></returns>
  121. public static IEnumerable<T> Find<T, U>(this IMongoCollection<T> collection, U template, int limit, string fullyQualifiedName)
  122. {
  123. return collection.Find(template, limit, 0, fullyQualifiedName);
  124. }
  125. /// <summary>
  126. /// Finds documents that match the template, and ordered according to the orderby document.
  127. /// </summary>
  128. /// <typeparam retval="U"></typeparam>
  129. /// <typeparam retval="S"></typeparam>
  130. /// <param retval="template">The spec document</param>
  131. /// <param retval="orderBy">The order specification</param>
  132. /// <returns>A set of documents ordered correctly and matching the spec.</returns>
  133. public static IEnumerable<T> Find<T, U, S>(this IMongoCollection<T> collection, U template, S orderBy)
  134. {
  135. return collection.Find(template, orderBy, Int32.MaxValue, 0, collection.FullyQualifiedName);
  136. }
  137. /// <summary>
  138. /// Find objects in the collection without any qualifiers.
  139. /// </summary>
  140. /// <returns></returns>
  141. public static IEnumerable<T> Find<T>(this IMongoCollection<T> collection)
  142. {
  143. // this is a hack to get a value that will test for null into the serializer.
  144. return collection.Find(new object(), Int32.MaxValue, collection.FullyQualifiedName);
  145. }
  146. /// <summary>
  147. /// Inserts documents
  148. /// </summary>
  149. /// <param retval="documentsToInsert">
  150. /// The documents to insert.
  151. /// </param>
  152. public static void Insert<T>(this IMongoCollection<T> collection, params T[] documentsToInsert)
  153. {
  154. collection.Insert(documentsToInsert.AsEnumerable());
  155. }
  156. public static T FindAndModify<T, U, X>(this IMongoCollection<T> collection, U query, X update)
  157. {
  158. return collection.FindAndModify<U, X, object>(query, update, new { });
  159. }
  160. /// <summary>
  161. /// Overload of Update that updates one document and doesn't upsert if no matches are found.
  162. /// </summary>
  163. /// <typeparam retval="X">Document to match</typeparam>
  164. /// <typeparam retval="U">Value document</typeparam>
  165. /// <param retval="matchDocument">The match Document.</param>
  166. /// <param retval="valueDocument">The value Document.</param>
  167. public static void UpdateOne<T, X, U>(this IMongoCollection<T> collection, X matchDocument, U valueDocument)
  168. {
  169. collection.Update(matchDocument, valueDocument, false, false);
  170. }
  171. /// <summary>Allows a document to be updated using the specified action.</summary>
  172. public static void Update<T, X>(this IMongoCollection<T> collection, X matchDocument, Action<IModifierExpression<T>> action)
  173. {
  174. collection.Update(matchDocument, action, false, false);
  175. }
  176. /// <summary>
  177. /// A count on this collection without any filter.
  178. /// </summary>
  179. /// <returns>The count.</returns>
  180. public static long Count<T>(this IMongoCollection<T> collection)
  181. {
  182. return collection.Count(new { });
  183. }
  184. /// <summary>
  185. /// Deletes all indices on this collection.
  186. /// </summary>
  187. /// <param retval="numberDeleted">
  188. /// </param>
  189. /// <returns>
  190. /// The delete indices.
  191. /// </returns>
  192. public static bool DeleteIndices<T>(this IMongoCollection<T> collection, out int numberDeleted)
  193. {
  194. return collection.DeleteIndex("*", out numberDeleted);
  195. }
  196. //public static IEnumerable<X> MapReduce<T, X>(this IMongoCollection<T> collection, string map, string reduce)
  197. //{
  198. // return collection.MapReduce<X>(new MapReduceOptions<T> { Map = map, Reduce = reduce });
  199. //}
  200. //public static IEnumerable<X> MapReduce<T, U, X>(this IMongoCollection<T> collection, U template, string map, string reduce)
  201. //{
  202. // return collection.MapReduce<X>(new MapReduceOptions<T>() { Query = template, Map = map, Reduce = reduce });
  203. //}
  204. //public static IEnumerable<X> MapReduce<T, U, X>(this IMongoCollection<T> collection, U template, string map, string reduce, string finalize)
  205. //{
  206. // return collection.MapReduce<X>(new MapReduceOptions<T> { Query = template, Map = map, Reduce = reduce, Finalize = finalize });
  207. //}
  208. }
  209. }