PageRenderTime 74ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NoRMDriverDemo/Program.cs

https://github.com/ChrisEdwards/IntroducingMongoDB
C# | 314 lines | 192 code | 68 blank | 54 comment | 3 complexity | 8d90ad58f569a99b3fd674a592dfae5a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Norm;
  5. using Norm.Collections;
  6. using Norm.Linq;
  7. namespace NoRMDriverDemo
  8. {
  9. /// <summary>
  10. /// The program.
  11. /// </summary>
  12. internal class Program
  13. {
  14. /// <summary>
  15. /// The main.
  16. /// </summary>
  17. /// <param name="args">
  18. /// The args.
  19. /// </param>
  20. private static void Main( string[] args )
  21. {
  22. var mongo = new Mongo( "myorders", "localhost", "27017", string.Empty );
  23. // For Linq to work, collection name must match Class name.
  24. MongoCollection< Order > collection = mongo.GetCollection< Order >( "Order" );
  25. var provider = new MongoQueryProvider(mongo);
  26. var orderQueryable = new MongoQuery< Order >( provider );
  27. DeleteAllDocumentsFromCollection( collection );
  28. CreateAndInsertSingleDocumentIntoCollection( collection );
  29. CreateAndInsertMultipleDocumentIntoCollection( collection );
  30. UpdateDocument( collection );
  31. UpdateUsingAtomicIncrement(collection);
  32. QueryFromCollection( collection );
  33. QueryUsingLinq( orderQueryable );
  34. QueryAllDocuments(orderQueryable);
  35. QueryConditionalOperators( collection );
  36. QueryConditionalOperatorsUsingLinq(orderQueryable);
  37. Console.WriteLine( "\n Press Enter to Exit." );
  38. Console.ReadLine();
  39. }
  40. private static void QueryConditionalOperators( MongoCollection< Order > collection )
  41. {
  42. Console.WriteLine("\n\n======= Query with Conditional Operators (Using Anonymous Objects) =======");
  43. var ordersWithAmountGreaterThan50 = collection.Find( new {OrderAmount = Q.GreaterThan( 50 )} );
  44. var ordersWithAmountGreaterThanOEqualTo50 = collection.Find( new {OrderAmount = Q.GreaterOrEqual( 50 )} );
  45. var ordersWithAmountLessThan100 = collection.Find( new {OrderAmount = Q.LessThan( 100 )} );
  46. var ordersWithAmountLessThanOEqualTo100 = collection.Find( new {OrderAmount = Q.LessOrEqual( 100 )} );
  47. var ordersWithAmountBetween50And200 = collection.Find( new {OrderAmount = Q.GreaterThan( 50 ).And(Q.LessThan( 200 ))} );
  48. var ordersWithAmountNotEqualTo100 = collection.Find( new {OrderAmount = Q.NotEqual( 100 )} );
  49. var ordersWithAmountInList = collection.Find( new {CustomerName = Q.In( "Elmer Fudd", "Daffy Duck" )} );
  50. Console.WriteLine("\nOrders with amount greater than 50:");
  51. foreach ( var order in ordersWithAmountGreaterThan50 )
  52. Console.WriteLine(order.ToString());
  53. Console.WriteLine("\nOrders with amount greater than or equal to 50:");
  54. foreach ( var order in ordersWithAmountGreaterThanOEqualTo50 )
  55. Console.WriteLine(order.ToString());
  56. Console.WriteLine("\nOrders with amount less than 100:");
  57. foreach ( var order in ordersWithAmountLessThan100 )
  58. Console.WriteLine(order.ToString());
  59. Console.WriteLine("\nOrders with amount less than or equal to 100:");
  60. foreach ( var order in ordersWithAmountLessThanOEqualTo100 )
  61. Console.WriteLine(order.ToString());
  62. Console.WriteLine("\nOrders with amount between 50 and 200:");
  63. foreach ( var order in ordersWithAmountBetween50And200 )
  64. Console.WriteLine(order.ToString());
  65. Console.WriteLine("\nOrders with amount not equal to 100:");
  66. foreach (var order in ordersWithAmountNotEqualTo100)
  67. Console.WriteLine(order.ToString());
  68. Console.WriteLine("\nOrders with amount in list:");
  69. foreach (var order in ordersWithAmountInList)
  70. Console.WriteLine(order.ToString());
  71. }
  72. private static void QueryConditionalOperatorsUsingLinq(MongoQuery<Order> orders)
  73. {
  74. Console.WriteLine("\n\n======= Query with Conditional Operators (Using Linq) =======");
  75. var ordersWithAmountGreaterThan50 = from order in orders
  76. where order.OrderAmount > 50
  77. select order;
  78. var ordersWithAmountGreaterThanOEqualTo50 = from order in orders
  79. where order.OrderAmount >= 50
  80. select order;
  81. var ordersWithAmountLessThan100 = from order in orders
  82. where order.OrderAmount < 100
  83. select order;
  84. var ordersWithAmountLessThanOEqualTo100 = from order in orders
  85. where order.OrderAmount <= 100
  86. select order;
  87. var ordersWithAmountBetween50And200 = from order in orders
  88. where order.OrderAmount > 50 && order.OrderAmount < 200
  89. select order;
  90. var ordersWithAmountNotEqualTo100= from order in orders
  91. where order.OrderAmount != 100
  92. select order;
  93. var ordersWithAmountInList = from order in orders
  94. where new List<string> {"Elmer Fudd", "Daffy Duck"}.Contains( order.CustomerName )
  95. select order;
  96. Console.WriteLine("\nOrders with amount greater than 50:");
  97. foreach ( var order in ordersWithAmountGreaterThan50 )
  98. Console.WriteLine(order.ToString());
  99. Console.WriteLine("\nOrders with amount greater than or equal to 50:");
  100. foreach ( var order in ordersWithAmountGreaterThanOEqualTo50 )
  101. Console.WriteLine(order.ToString());
  102. Console.WriteLine("\nOrders with amount less than 100:");
  103. foreach ( var order in ordersWithAmountLessThan100 )
  104. Console.WriteLine(order.ToString());
  105. Console.WriteLine("\nOrders with amount less than or equal to 100:");
  106. foreach ( var order in ordersWithAmountLessThanOEqualTo100 )
  107. Console.WriteLine(order.ToString());
  108. Console.WriteLine("\nOrders with amount between 50 and 200:");
  109. foreach ( var order in ordersWithAmountBetween50And200 )
  110. Console.WriteLine(order.ToString());
  111. Console.WriteLine("\nOrders with amount not equal to 100:");
  112. foreach ( var order in ordersWithAmountNotEqualTo100 )
  113. Console.WriteLine(order.ToString());
  114. Console.WriteLine("\nOrders with amount in list:");
  115. foreach ( var order in ordersWithAmountInList )
  116. Console.WriteLine(order.ToString());
  117. }
  118. /// <summary>
  119. /// Demo deleting documents from collection.
  120. /// </summary>
  121. /// <param name="orders">The orders.</param>
  122. private static void DeleteAllDocumentsFromCollection(MongoCollection<Order> collection)
  123. {
  124. Console.WriteLine("\n\n======= Delete Documents =======");
  125. Console.WriteLine(string.Format("Document Count Before Delete: [ {0} ]", collection.Count()));
  126. // Delete documents matching a criteria.
  127. collection.Delete( new {CustomerName = "Elmer Fudd"} );
  128. Console.WriteLine(string.Format("Document Count After Deleting Elmer Fudd: [ {0} ]", collection.Count()));
  129. // Delete all docs.
  130. collection.Delete( new {} );
  131. Console.WriteLine("Deleted all docs");
  132. Console.WriteLine(string.Format("Document Count After Deleting All Docs: [ {0} ]", collection.Count()));
  133. }
  134. /// <summary>
  135. /// Inserts a new document.
  136. /// </summary>
  137. /// <param name="collection">The orders.</param>
  138. private static void CreateAndInsertSingleDocumentIntoCollection(MongoCollection<Order> collection)
  139. {
  140. Console.WriteLine("\n\n======= Insert Multiple Documents =======");
  141. Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", collection.Count()));
  142. // Create a new order.
  143. var order = new Order()
  144. {
  145. OrderAmount = 57.22,
  146. CustomerName = "Elmer Fudd"
  147. };
  148. // Add the new order to the mongo orders colleciton.
  149. collection.Insert(order);
  150. Console.WriteLine(string.Format("Inserted: {0}", order));
  151. Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", collection.Count()));
  152. }
  153. /// <summary>
  154. /// Demo inserting multiple document into collection.
  155. /// </summary>
  156. /// <param name="orders">The orders.</param>
  157. private static void CreateAndInsertMultipleDocumentIntoCollection(MongoCollection<Order> orders)
  158. {
  159. Console.WriteLine("\n\n======= Insert Multiple Documents =======");
  160. Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", orders.Count()));
  161. // Create new orders.
  162. var order1 = new Order
  163. {
  164. OrderAmount = 100.23,
  165. CustomerName = "Bugs Bunny"
  166. };
  167. var order2 = new Order
  168. {
  169. OrderAmount = 0.01,
  170. CustomerName = "Daffy Duck"
  171. };
  172. IEnumerable<Order> orderList = new List<Order> { order1, order2 };
  173. // Insert an IEnumerable.
  174. orders.Insert(orderList);
  175. Console.WriteLine(string.Format("Inserted: {0}", order1));
  176. Console.WriteLine(string.Format("Inserted: {0}", order2));
  177. Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", orders.Count()));
  178. }
  179. /// <summary>
  180. /// Demo of Updating a document.
  181. /// </summary>
  182. /// <param name="orders">The orders.</param>
  183. private static void UpdateDocument(MongoCollection<Order> orders)
  184. {
  185. Console.WriteLine("\n\n======= Update Documents =======");
  186. var selector = new {CustomerName = "Daffy Duck"};
  187. var orderToUpdate = orders.FindOne(selector);
  188. Console.WriteLine("Before Update: " + orderToUpdate);
  189. // I'm in the money!
  190. orderToUpdate.OrderAmount = 1000000.00;
  191. // Update Daffy's account before Hasaan finds him.
  192. orders.Save( orderToUpdate );
  193. Console.WriteLine("After Update: " + orders.FindOne(selector));
  194. }
  195. /// <summary>
  196. /// Demoes Updates a single field using an atomic Increment ($inc) operator.
  197. /// </summary>
  198. /// <param name="orders">The orders.</param>
  199. private static void UpdateUsingAtomicIncrement( MongoCollection< Order > orders )
  200. {
  201. Console.WriteLine("\n\n======= Update Document using Increment =======");
  202. var selector = new { CustomerName = "Daffy Duck" };
  203. Console.WriteLine("Before Update: " + orders.FindOne(selector));
  204. // Add 2000 to order amount on document matching selector.
  205. orders.Update( selector, x=>x.Increment( o=>o.OrderAmount, 2000 ) );
  206. Console.WriteLine("After Update: " + orders.FindOne(selector));
  207. }
  208. /// <summary>
  209. /// Demos querying the collection.
  210. /// </summary>
  211. /// <param name="orders">The orders.</param>
  212. private static void QueryFromCollection(MongoCollection<Order> orders)
  213. {
  214. Console.WriteLine("\n\n======= Query One Document =======");
  215. // Create a specification to query the orders collection.
  216. // Run the query.
  217. Order result = orders.FindOne(new {});
  218. Console.WriteLine(string.Format("Found: {0}", result));
  219. }
  220. /// <summary>
  221. /// Demo querying the collection with Linq.
  222. /// </summary>
  223. /// <param name="orders">The orders.</param>
  224. private static void QueryUsingLinq(MongoQuery<Order> orders)
  225. {
  226. Console.WriteLine("\n\n======= Query Using Linq =======");
  227. // Query the orders collection.
  228. IQueryable<Order> results =
  229. from order in orders
  230. where order.CustomerName == "Elmer Fudd"
  231. select order;
  232. var result = results.FirstOrDefault();
  233. Console.WriteLine(string.Format("Found: {0}", result));
  234. }
  235. /// <summary>
  236. /// Demo of Querying the collection.
  237. /// </summary>
  238. /// <param name="collection">The collection.</param>
  239. private static void QueryAllDocuments(MongoQuery<Order> orders)
  240. {
  241. Console.WriteLine( "\n\n======= Query All Documents =======" );
  242. foreach ( Order order in orders )
  243. Console.WriteLine( string.Format( "Found: {0}", order ) );
  244. }
  245. }
  246. }