diff --git a/source/includes/code-examples/delete-many/DeleteMany.cs b/source/includes/code-examples/delete-many/DeleteMany.cs index 49513ba8..96228210 100644 --- a/source/includes/code-examples/delete-many/DeleteMany.cs +++ b/source/includes/code-examples/delete-many/DeleteMany.cs @@ -14,20 +14,26 @@ public class DeleteMany public static void Main(string[] args) { - Setup(); + try { + Setup(); - var filter = Builders.Filter - .Eq(r => r.Borough, "Brooklyn"); + var filter = Builders.Filter + .Eq(r => r.Borough, "Brooklyn"); - var docs = _restaurantsCollection.Find(filter).ToList(); + var docs = _restaurantsCollection.Find(filter).ToList(); - // Deletes documents by using builders - Console.WriteLine("Deleting documents..."); - var result = DeleteMultipleRestaurantsBuilder(); + // Deletes documents by using builders + Console.WriteLine("Deleting documents..."); + var result = DeleteMultipleRestaurantsBuilder(); - Console.WriteLine($"Deleted documents: {result.DeletedCount}"); + Console.WriteLine($"Deleted documents: {result.DeletedCount}"); - Restore(docs); + Restore(docs); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to delete due to an error: " + me); + } } // Deletes all documents that have a Borough value of "Brooklyn" diff --git a/source/includes/code-examples/delete-many/DeleteManyAsync.cs b/source/includes/code-examples/delete-many/DeleteManyAsync.cs index e3561fdd..fd220a7d 100644 --- a/source/includes/code-examples/delete-many/DeleteManyAsync.cs +++ b/source/includes/code-examples/delete-many/DeleteManyAsync.cs @@ -14,20 +14,26 @@ public class DeleteManyAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - var filter = Builders.Filter - .Eq(r => r.Borough, "Brooklyn"); + var filter = Builders.Filter + .Eq(r => r.Borough, "Brooklyn"); - var docs = _restaurantsCollection.Find(filter).ToList(); + var docs = _restaurantsCollection.Find(filter).ToList(); - // Deletes documents by using builders - Console.WriteLine("Deleting documents..."); - var result = await DeleteMultipleRestaurantsBuilderAsync(); + // Deletes documents by using builders + Console.WriteLine("Deleting documents..."); + var result = await DeleteMultipleRestaurantsBuilderAsync(); - Console.WriteLine($"Deleted documents: {result.DeletedCount}"); + Console.WriteLine($"Deleted documents: {result.DeletedCount}"); - Restore(docs); + Restore(docs); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to delete due to an error: " + me); + } } // Deletes all documents that have a Borough value of "Brooklyn" diff --git a/source/includes/code-examples/delete-one/DeleteOne.cs b/source/includes/code-examples/delete-one/DeleteOne.cs index 12e5bc84..24794a55 100644 --- a/source/includes/code-examples/delete-one/DeleteOne.cs +++ b/source/includes/code-examples/delete-one/DeleteOne.cs @@ -14,20 +14,26 @@ public class DeleteOne public static void Main(string[] args) { - Setup(); + try { + Setup(); - var filter = Builders.Filter - .Eq(r => r.Name, "Ready Penny Inn"); + var filter = Builders.Filter + .Eq(r => r.Name, "Ready Penny Inn"); + + var doc = _restaurantsCollection.Find(filter).First(); - var doc = _restaurantsCollection.Find(filter).First(); + // Deletes a document by using builders + Console.WriteLine("Deleting a document with builders..."); + var result = DeleteARestaurantBuilder(); - // Deletes a document by using builders - Console.WriteLine("Deleting a document with builders..."); - var result = DeleteARestaurantBuilder(); + Console.WriteLine($"Deleted documents: {result.DeletedCount}"); - Console.WriteLine($"Deleted documents: {result.DeletedCount}"); + Restore(doc); - Restore(doc); + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to delete due to an error: " + me); + } } private static DeleteResult DeleteARestaurantBuilder() diff --git a/source/includes/code-examples/delete-one/DeleteOneAsync.cs b/source/includes/code-examples/delete-one/DeleteOneAsync.cs index d71b7790..3bf0be2d 100644 --- a/source/includes/code-examples/delete-one/DeleteOneAsync.cs +++ b/source/includes/code-examples/delete-one/DeleteOneAsync.cs @@ -14,20 +14,26 @@ public class DeleteOneAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - var filter = Builders.Filter - .Eq(r => r.Name, "Ready Penny Inn"); + var filter = Builders.Filter + .Eq(r => r.Name, "Ready Penny Inn"); + + var doc = _restaurantsCollection.Find(filter).First(); - var doc = _restaurantsCollection.Find(filter).First(); + // Deletes a document by using builders + Console.WriteLine("Deleting a document with builders..."); + var result = await DeleteARestaurantBuilderAsync(); - // Deletes a document by using builders - Console.WriteLine("Deleting a document with builders..."); - var result = await DeleteARestaurantBuilderAsync(); + Console.WriteLine($"Deleted documents: {result.DeletedCount}"); - Console.WriteLine($"Deleted documents: {result.DeletedCount}"); + Restore(doc); - Restore(doc); + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to delete due to an error: " + me); + } } private static async Task DeleteARestaurantBuilderAsync() diff --git a/source/includes/code-examples/find-many/FindMany.cs b/source/includes/code-examples/find-many/FindMany.cs index 55bd3048..cf0a0347 100644 --- a/source/includes/code-examples/find-many/FindMany.cs +++ b/source/includes/code-examples/find-many/FindMany.cs @@ -14,27 +14,33 @@ public class FindMany public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Finds multiple documents by using builders - Console.WriteLine("Finding documents with builders...:"); - var restaurants = FindMultipleRestaurantsBuilderSync(); - Console.WriteLine($"Number of documents found: {restaurants.Count}"); + // Finds multiple documents by using builders + Console.WriteLine("Finding documents with builders...:"); + var restaurants = FindMultipleRestaurantsBuilderSync(); + Console.WriteLine($"Number of documents found: {restaurants.Count}"); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Retrieves multiple documents by using LINQ - Console.WriteLine("Finding documents with LINQ...:"); - restaurants = FindMultipleRestaurantsLinqSync(); - Console.WriteLine($"Number of documents found: {restaurants.Count}"); + // Retrieves multiple documents by using LINQ + Console.WriteLine("Finding documents with LINQ...:"); + restaurants = FindMultipleRestaurantsLinqSync(); + Console.WriteLine($"Number of documents found: {restaurants.Count}"); - Console.WriteLine(); + Console.WriteLine(); - // Retrieves all documents in the "restaurants" collection - Console.WriteLine("Finding all documents...:"); - restaurants = FindAllRestaurantsSync(); - Console.WriteLine($"Number of documents found: {restaurants.Count}"); + // Retrieves all documents in the "restaurants" collection + Console.WriteLine("Finding all documents...:"); + restaurants = FindAllRestaurantsSync(); + Console.WriteLine($"Number of documents found: {restaurants.Count}"); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to find due to an error: " + me); + } } public static List FindMultipleRestaurantsBuilderSync() diff --git a/source/includes/code-examples/find-many/FindManyAsync.cs b/source/includes/code-examples/find-many/FindManyAsync.cs index 752b6074..6f5804a4 100644 --- a/source/includes/code-examples/find-many/FindManyAsync.cs +++ b/source/includes/code-examples/find-many/FindManyAsync.cs @@ -15,27 +15,33 @@ public class FindManyAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - // Finds multiple documents by using builders - Console.WriteLine("Finding documents with builders...:"); - var restaurantsBuilders = await FindMultipleRestaurantsBuilderAsync(); - Console.WriteLine($"Number of documents found: {restaurantsBuilders.Count}"); + // Finds multiple documents by using builders + Console.WriteLine("Finding documents with builders...:"); + var restaurantsBuilders = await FindMultipleRestaurantsBuilderAsync(); + Console.WriteLine($"Number of documents found: {restaurantsBuilders.Count}"); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Retrieves multiple documents by using LINQ - Console.WriteLine("Finding documents with LINQ...:"); - var restaurantsLinq = await FindMultipleRestaurantsLinqAsync(); - Console.WriteLine($"Number of documents found: {restaurantsLinq.Count}"); + // Retrieves multiple documents by using LINQ + Console.WriteLine("Finding documents with LINQ...:"); + var restaurantsLinq = await FindMultipleRestaurantsLinqAsync(); + Console.WriteLine($"Number of documents found: {restaurantsLinq.Count}"); - Console.WriteLine(); + Console.WriteLine(); - // Retrieves all documents in the "restaurants" collection - Console.WriteLine("Finding all documents...:"); - var allRestaurants = await FindAllRestaurantsAsync(); - Console.WriteLine($"Number of documents found: {allRestaurants.Count}"); + // Retrieves all documents in the "restaurants" collection + Console.WriteLine("Finding all documents...:"); + var allRestaurants = await FindAllRestaurantsAsync(); + Console.WriteLine($"Number of documents found: {allRestaurants.Count}"); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to find due to an error: " + me); + } } private static async Task> FindMultipleRestaurantsBuilderAsync() diff --git a/source/includes/code-examples/find-one/FindOne.cs b/source/includes/code-examples/find-one/FindOne.cs index d855b0b4..e633f70e 100644 --- a/source/includes/code-examples/find-one/FindOne.cs +++ b/source/includes/code-examples/find-one/FindOne.cs @@ -14,18 +14,24 @@ public class FindOne public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Finds one document by using builders - Console.WriteLine("Finding a document with builders..."); - FindOneRestaurantBuilder(); + // Finds one document by using builders + Console.WriteLine("Finding a document with builders..."); + FindOneRestaurantBuilder(); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Finds one document by using LINQ - Console.WriteLine("Finding a document with LINQ..."); - FindOneRestaurantLinq(); + // Finds one document by using LINQ + Console.WriteLine("Finding a document with LINQ..."); + FindOneRestaurantLinq(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to find due to an error: " + me); + } } private static void FindOneRestaurantBuilder() diff --git a/source/includes/code-examples/find-one/FindOneAsync.cs b/source/includes/code-examples/find-one/FindOneAsync.cs index 9c3d2043..37af513c 100644 --- a/source/includes/code-examples/find-one/FindOneAsync.cs +++ b/source/includes/code-examples/find-one/FindOneAsync.cs @@ -15,20 +15,26 @@ public class FindOneAsync public static async Task Main(string[] args) { - Setup(); - - // Finds one document by using builders - var buildersDocument = await FindOneRestaurantBuilderAsync(); - Console.WriteLine("Finding a document with builders..."); - Console.WriteLine(buildersDocument.ToBsonDocument()); - - // Prints extra space for console readability - Console.WriteLine(); - - // Finds one document by using LINQ - var linqDocument = await FindOneRestaurantLinqAsync(); - Console.WriteLine("Finding a document with LINQ..."); - Console.WriteLine(linqDocument.ToBsonDocument()); + try { + Setup(); + + // Finds one document by using builders + var buildersDocument = await FindOneRestaurantBuilderAsync(); + Console.WriteLine("Finding a document with builders..."); + Console.WriteLine(buildersDocument.ToBsonDocument()); + + // Prints extra space for console readability + Console.WriteLine(); + + // Finds one document by using LINQ + var linqDocument = await FindOneRestaurantLinqAsync(); + Console.WriteLine("Finding a document with LINQ..."); + Console.WriteLine(linqDocument.ToBsonDocument()); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to find due to an error: " + me); + } } private static async Task FindOneRestaurantBuilderAsync() diff --git a/source/includes/code-examples/insert-many/InsertMany.cs b/source/includes/code-examples/insert-many/InsertMany.cs index b83bdac7..8f45c7f6 100644 --- a/source/includes/code-examples/insert-many/InsertMany.cs +++ b/source/includes/code-examples/insert-many/InsertMany.cs @@ -14,32 +14,38 @@ public class InsertMany public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" - var filter = Builders.Filter - .Eq(r => r.Name, "Mongo's Pizza"); + // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" + var filter = Builders.Filter + .Eq(r => r.Name, "Mongo's Pizza"); - // Finds all documents that match the filter - var foundRestaurants = _restaurantsCollection.Find(filter).ToList(); + // Finds all documents that match the filter + var foundRestaurants = _restaurantsCollection.Find(filter).ToList(); - Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}"); + Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}"); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - Console.WriteLine("Inserting documents..."); + Console.WriteLine("Inserting documents..."); - // Inserts the documents by using a helper method - InsertManyRestaurants(); + // Inserts the documents by using a helper method + InsertManyRestaurants(); - // Finds all documents that match the filter after the insert - foundRestaurants = _restaurantsCollection.Find(filter).ToList(); + // Finds all documents that match the filter after the insert + foundRestaurants = _restaurantsCollection.Find(filter).ToList(); - // Prints the number of documents found - Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}"); + // Prints the number of documents found + Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}"); - Cleanup(); + Cleanup(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to insert due to an error: " + me); + } } private static void InsertManyRestaurants() diff --git a/source/includes/code-examples/insert-many/InsertManyAsync.cs b/source/includes/code-examples/insert-many/InsertManyAsync.cs index f01524fa..9f8adbc4 100644 --- a/source/includes/code-examples/insert-many/InsertManyAsync.cs +++ b/source/includes/code-examples/insert-many/InsertManyAsync.cs @@ -14,33 +14,39 @@ public class InsertManyAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" - var filter = Builders.Filter - .Eq(r => r.Name, "Mongo's Pizza"); + // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" + var filter = Builders.Filter + .Eq(r => r.Name, "Mongo's Pizza"); - // Finds all documents that match the filter - var foundRestaurants = _restaurantsCollection.Find(filter).ToList(); + // Finds all documents that match the filter + var foundRestaurants = _restaurantsCollection.Find(filter).ToList(); - // Prints the number of documents found - Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}"); + // Prints the number of documents found + Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}"); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - Console.WriteLine("Inserting documents..."); + Console.WriteLine("Inserting documents..."); - // Asynchronously inserts the documents by using a helper method - await InsertManyRestaurantsAsync(); + // Asynchronously inserts the documents by using a helper method + await InsertManyRestaurantsAsync(); - // Finds all documents that match the filter after the insert - foundRestaurants = _restaurantsCollection.Find(filter).ToList(); + // Finds all documents that match the filter after the insert + foundRestaurants = _restaurantsCollection.Find(filter).ToList(); - // Prints the number of documents found - Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}"); + // Prints the number of documents found + Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}"); - Cleanup(); + Cleanup(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to insert due to an error: " + me); + } } private static async Task InsertManyRestaurantsAsync() diff --git a/source/includes/code-examples/insert-one/InsertOne.cs b/source/includes/code-examples/insert-one/InsertOne.cs index 1e068d6e..4a2a32b4 100644 --- a/source/includes/code-examples/insert-one/InsertOne.cs +++ b/source/includes/code-examples/insert-one/InsertOne.cs @@ -14,22 +14,28 @@ public class InsertOne public static void Main(string[] args) { - Setup(); + try { + Setup(); - Console.WriteLine("Inserting a document..."); - InsertOneRestaurant(); + Console.WriteLine("Inserting a document..."); + InsertOneRestaurant(); - // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" - var filter = Builders.Filter - .Eq(r => r.Name, "Mongo's Pizza"); + // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" + var filter = Builders.Filter + .Eq(r => r.Name, "Mongo's Pizza"); - // Finds the newly inserted document by using the filter - var document = _restaurantsCollection.Find(filter).FirstOrDefault(); + // Finds the newly inserted document by using the filter + var document = _restaurantsCollection.Find(filter).FirstOrDefault(); - // Prints the document - Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}"); + // Prints the document + Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}"); - Cleanup(); + Cleanup(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to insert due to an error: " + me); + } } private static void InsertOneRestaurant() diff --git a/source/includes/code-examples/insert-one/InsertOneAsync.cs b/source/includes/code-examples/insert-one/InsertOneAsync.cs index 488c5bdf..cce48a05 100644 --- a/source/includes/code-examples/insert-one/InsertOneAsync.cs +++ b/source/includes/code-examples/insert-one/InsertOneAsync.cs @@ -14,22 +14,28 @@ public class InsertOneAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - Console.WriteLine("Inserting a document..."); - await InsertOneRestaurantAsync(); + Console.WriteLine("Inserting a document..."); + await InsertOneRestaurantAsync(); - // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" - var filter = Builders.Filter - .Eq(r => r.Name, "Mongo's Pizza"); + // Creates a filter for all documents that have a "name" value of "Mongo's Pizza" + var filter = Builders.Filter + .Eq(r => r.Name, "Mongo's Pizza"); - // Finds the newly inserted document by using the filter - var document = _restaurantsCollection.Find(filter).FirstOrDefault(); + // Finds the newly inserted document by using the filter + var document = _restaurantsCollection.Find(filter).FirstOrDefault(); - // Prints the document - Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}"); + // Prints the document + Console.WriteLine($"Document Inserted: {document.ToBsonDocument()}"); - Cleanup(); + Cleanup(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to insert due to an error: " + me); + } } private static async Task InsertOneRestaurantAsync() diff --git a/source/includes/code-examples/replace-one/ReplaceOne.cs b/source/includes/code-examples/replace-one/ReplaceOne.cs index 70fe8bcf..cb68232e 100644 --- a/source/includes/code-examples/replace-one/ReplaceOne.cs +++ b/source/includes/code-examples/replace-one/ReplaceOne.cs @@ -14,26 +14,32 @@ public class ReplaceOne public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza" - var filter = Builders.Filter - .Eq(r => r.Cuisine, "Pizza"); + // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza" + var filter = Builders.Filter + .Eq(r => r.Cuisine, "Pizza"); - // Finds the first restaurant document that matches the filter - var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); - Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}"); + // Finds the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}"); + + // Replaces the document by using a helper method + var syncResult = ReplaceOneRestaurant(); + Console.WriteLine($"Restaurants modified by replacement: {syncResult.ModifiedCount}"); - // Replaces the document by using a helper method - var syncResult = ReplaceOneRestaurant(); - Console.WriteLine($"Restaurants modified by replacement: {syncResult.ModifiedCount}"); + var firstPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + Console.WriteLine($"First pizza restaurant after replacement: {firstPizzaRestaurant.Name}"); - var firstPizzaRestaurant = _restaurantsCollection.Find(filter).First(); - Console.WriteLine($"First pizza restaurant after replacement: {firstPizzaRestaurant.Name}"); + Console.WriteLine("Resetting sample data..."); + _restaurantsCollection.ReplaceOneAsync(filter, oldPizzaRestaurant); + Console.WriteLine("done."); - Console.WriteLine("Resetting sample data..."); - _restaurantsCollection.ReplaceOneAsync(filter, oldPizzaRestaurant); - Console.WriteLine("done."); + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to replace due to an error: " + me); + } } private static ReplaceOneResult ReplaceOneRestaurant() diff --git a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs index 03512641..2bba474c 100644 --- a/source/includes/code-examples/replace-one/ReplaceOneAsync.cs +++ b/source/includes/code-examples/replace-one/ReplaceOneAsync.cs @@ -14,26 +14,32 @@ public class ReplaceOneAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza" - var filter = Builders.Filter - .Eq(r => r.Cuisine, "Pizza"); + // Creates a filter for all restaurant documents that have a "cuisine" value of "Pizza" + var filter = Builders.Filter + .Eq(r => r.Cuisine, "Pizza"); - // Finds the first restaurant document that matches the filter - var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); - Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}"); + // Finds the first restaurant document that matches the filter + var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + Console.WriteLine($"First pizza restaurant before replacement: {oldPizzaRestaurant.Name}"); + + // Asynchronously replaces the document by using a helper method + var asyncResult = await ReplaceOneRestaurant(); + Console.WriteLine($"Restaurants modified by replacement: {asyncResult.ModifiedCount}"); - // Asynchronously replaces the document by using a helper method - var asyncResult = await ReplaceOneRestaurant(); - Console.WriteLine($"Restaurants modified by replacement: {asyncResult.ModifiedCount}"); + var firstPizzaRestaurant = _restaurantsCollection.Find(filter).First(); + Console.WriteLine($"First pizza restaurant after replacement: {firstPizzaRestaurant.Name}"); - var firstPizzaRestaurant = _restaurantsCollection.Find(filter).First(); - Console.WriteLine($"First pizza restaurant after replacement: {firstPizzaRestaurant.Name}"); + Console.WriteLine("Resetting sample data..."); + await _restaurantsCollection.ReplaceOneAsync(filter, oldPizzaRestaurant); + Console.WriteLine("done."); - Console.WriteLine("Resetting sample data..."); - await _restaurantsCollection.ReplaceOneAsync(filter, oldPizzaRestaurant); - Console.WriteLine("done."); + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to replace due to an error: " + me); + } } private static async Task ReplaceOneRestaurant() diff --git a/source/includes/code-examples/update-many/UpdateMany.cs b/source/includes/code-examples/update-many/UpdateMany.cs index 10910302..b8eb7317 100644 --- a/source/includes/code-examples/update-many/UpdateMany.cs +++ b/source/includes/code-examples/update-many/UpdateMany.cs @@ -18,25 +18,31 @@ public class UpdateMany public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Finds the number of restaurants with a "cuisine" value of "Pizza" - Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}"); + // Finds the number of restaurants with a "cuisine" value of "Pizza" + Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}"); - // Updates many documents by using a helper method - var syncResult = UpdateManyRestaurants(); - Console.WriteLine($"Restaurants modified by update: {syncResult.ModifiedCount}"); + // Updates many documents by using a helper method + var syncResult = UpdateManyRestaurants(); + Console.WriteLine($"Restaurants modified by update: {syncResult.ModifiedCount}"); - // Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks" - Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}"); + // Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks" + Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}"); - // Resets the sample data - Console.WriteLine("Resetting sample data..."); - ResetSampleData(); - Console.WriteLine("done."); + // Resets the sample data + Console.WriteLine("Resetting sample data..."); + ResetSampleData(); + Console.WriteLine("done."); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to update due to an error: " + me); + } } private static UpdateResult UpdateManyRestaurants() diff --git a/source/includes/code-examples/update-many/UpdateManyAsync.cs b/source/includes/code-examples/update-many/UpdateManyAsync.cs index a4a84209..172a1868 100644 --- a/source/includes/code-examples/update-many/UpdateManyAsync.cs +++ b/source/includes/code-examples/update-many/UpdateManyAsync.cs @@ -18,25 +18,31 @@ public class UpdateManyAsync public static async Task Main(string[] args) { - Setup(); - - // Prints extra space for console readability - Console.WriteLine(); - - // Finds the number of restaurants with a "cuisine" value of "Pizza" - Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}"); - - // Asynchronously updates many documents by using a helper method - var asyncResult = await UpdateManyRestaurantsAsync(); - Console.WriteLine($"Restaurants modified by update: {asyncResult.ModifiedCount}"); - - // Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks" - Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}"); - - // Resets the sample data - Console.WriteLine("Resetting sample data..."); - ResetSampleData(); - Console.WriteLine("done."); + try { + Setup(); + + // Prints extra space for console readability + Console.WriteLine(); + + // Finds the number of restaurants with a "cuisine" value of "Pizza" + Console.WriteLine($"Restaurants with {CuisineField} \"{OldCuisine}\" found: {FindCountOfRestaurantsWithCuisine(OldCuisine)}"); + + // Asynchronously updates many documents by using a helper method + var asyncResult = await UpdateManyRestaurantsAsync(); + Console.WriteLine($"Restaurants modified by update: {asyncResult.ModifiedCount}"); + + // Finds the number of restaurants with a "cuisine" value of "Pasta and breadsticks" + Console.WriteLine($"Restaurants with {CuisineField} \"{NewCuisine}\" found after update: {FindCountOfRestaurantsWithCuisine(NewCuisine)}"); + + // Resets the sample data + Console.WriteLine("Resetting sample data..."); + ResetSampleData(); + Console.WriteLine("done."); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to update due to an error: " + me); + } } private static async Task UpdateManyRestaurantsAsync() diff --git a/source/includes/code-examples/update-one/UpdateOne.cs b/source/includes/code-examples/update-one/UpdateOne.cs index 9687af70..1c1ded85 100644 --- a/source/includes/code-examples/update-one/UpdateOne.cs +++ b/source/includes/code-examples/update-one/UpdateOne.cs @@ -14,15 +14,21 @@ public class UpdateOne public static void Main(string[] args) { - Setup(); + try { + Setup(); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Updates one document by using a helper method - var syncResult = UpdateOneRestaurant(); - Console.WriteLine($"Updated documents: {syncResult.ModifiedCount}"); - ResetSampleData(); + // Updates one document by using a helper method + var syncResult = UpdateOneRestaurant(); + Console.WriteLine($"Updated documents: {syncResult.ModifiedCount}"); + ResetSampleData(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException me) { + Console.WriteLine("Unable to update due to an error: " + me); + } } private static UpdateResult UpdateOneRestaurant() diff --git a/source/includes/code-examples/update-one/UpdateOneAsync.cs b/source/includes/code-examples/update-one/UpdateOneAsync.cs index c6521b38..166174dc 100644 --- a/source/includes/code-examples/update-one/UpdateOneAsync.cs +++ b/source/includes/code-examples/update-one/UpdateOneAsync.cs @@ -14,15 +14,21 @@ public class UpdateOneAsync public static async Task Main(string[] args) { - Setup(); + try { + Setup(); - // Prints extra space for console readability - Console.WriteLine(); + // Prints extra space for console readability + Console.WriteLine(); - // Updates one document asynchronously by using a helper method - var asyncResult = await UpdateOneRestaurantAsync(); - Console.WriteLine($"Updated documents: {asyncResult.ModifiedCount}"); - ResetSampleData(); + // Updates one document asynchronously by using a helper method + var asyncResult = await UpdateOneRestaurantAsync(); + Console.WriteLine($"Updated documents: {asyncResult.ModifiedCount}"); + ResetSampleData(); + + // Prints a message if any exceptions occur during the operation + } catch (MongoException e) { + Console.WriteLine("Unable to update due to an error: " + me); + } } private static async Task UpdateOneRestaurantAsync()