Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-37266: Add try-catch logic to code samples #188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions source/includes/code-examples/delete-many/DeleteMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ public class DeleteMany

public static void Main(string[] args)
{
Setup();
try {
Setup();

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Borough, "Brooklyn");
var filter = Builders<Restaurant>.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"
Expand Down
24 changes: 15 additions & 9 deletions source/includes/code-examples/delete-many/DeleteManyAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ public class DeleteManyAsync

public static async Task Main(string[] args)
{
Setup();
try {
Setup();

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Borough, "Brooklyn");
var filter = Builders<Restaurant>.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"
Expand Down
24 changes: 15 additions & 9 deletions source/includes/code-examples/delete-one/DeleteOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ public class DeleteOne

public static void Main(string[] args)
{
Setup();
try {
Setup();

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Name, "Ready Penny Inn");
var filter = Builders<Restaurant>.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()
Expand Down
24 changes: 15 additions & 9 deletions source/includes/code-examples/delete-one/DeleteOneAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ public class DeleteOneAsync

public static async Task Main(string[] args)
{
Setup();
try {
Setup();

var filter = Builders<Restaurant>.Filter
.Eq(r => r.Name, "Ready Penny Inn");
var filter = Builders<Restaurant>.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<DeleteResult> DeleteARestaurantBuilderAsync()
Expand Down
38 changes: 22 additions & 16 deletions source/includes/code-examples/find-many/FindMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Restaurant> FindMultipleRestaurantsBuilderSync()
Expand Down
38 changes: 22 additions & 16 deletions source/includes/code-examples/find-many/FindManyAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Restaurant>> FindMultipleRestaurantsBuilderAsync()
Expand Down
24 changes: 15 additions & 9 deletions source/includes/code-examples/find-one/FindOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 20 additions & 14 deletions source/includes/code-examples/find-one/FindOneAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Restaurant> FindOneRestaurantBuilderAsync()
Expand Down
40 changes: 23 additions & 17 deletions source/includes/code-examples/insert-many/InsertMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Restaurant>.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<Restaurant>.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()
Expand Down
Loading
Loading