/// <summary> /// Demo inserting multiple document into collection. /// </summary> /// <param name="orders">The orders.</param> private static void CreateAndInsertMultipleDocumentIntoCollection(MongoCollection<Order> orders) { Console.WriteLine("\n\n======= Insert Multiple Documents ======="); Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", orders.Count())); // Create new orders. var order1 = new Order { OrderAmount = 100.23, CustomerName = "Bugs Bunny" }; var order2 = new Order { OrderAmount = 0.01, CustomerName = "Daffy Duck" }; IEnumerable<Order> orderList = new List<Order> { order1, order2 }; // Insert an IEnumerable. orders.Insert(orderList); Console.WriteLine(string.Format("Inserted: {0}", order1)); Console.WriteLine(string.Format("Inserted: {0}", order2)); Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", orders.Count())); }
/// <summary> /// Demo deleting documents from collection. /// </summary> /// <param name="orders">The orders.</param> private static void DeleteAllDocumentsFromCollection(MongoCollection<Order> collection) { Console.WriteLine("\n\n======= Delete Documents ======="); Console.WriteLine(string.Format("Document Count Before Delete: [ {0} ]", collection.Count())); // Delete documents matching a criteria. collection.Delete( new {CustomerName = "Elmer Fudd"} ); Console.WriteLine(string.Format("Document Count After Deleting Elmer Fudd: [ {0} ]", collection.Count())); // Delete all docs. collection.Delete( new {} ); Console.WriteLine("Deleted all docs"); Console.WriteLine(string.Format("Document Count After Deleting All Docs: [ {0} ]", collection.Count())); }
/// <summary> /// Inserts a new document. /// </summary> /// <param name="collection">The orders.</param> private static void CreateAndInsertSingleDocumentIntoCollection(MongoCollection<Order> collection) { Console.WriteLine("\n\n======= Insert Multiple Documents ======="); Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", collection.Count())); // Create a new order. var order = new Order() { OrderAmount = 57.22, CustomerName = "Elmer Fudd" }; // Add the new order to the mongo orders colleciton. collection.Insert(order); Console.WriteLine(string.Format("Inserted: {0}", order)); Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", collection.Count())); }