Skip to content

Commit 7afd6d0

Browse files
authored
DOCSP-37266: Add try-catch logic to code samples (#188)
1 parent 6577f1e commit 7afd6d0

File tree

18 files changed

+333
-225
lines changed

18 files changed

+333
-225
lines changed

source/includes/code-examples/delete-many/DeleteMany.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@ public class DeleteMany
1414

1515
public static void Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
var filter = Builders<Restaurant>.Filter
20-
.Eq(r => r.Borough, "Brooklyn");
20+
var filter = Builders<Restaurant>.Filter
21+
.Eq(r => r.Borough, "Brooklyn");
2122

22-
var docs = _restaurantsCollection.Find(filter).ToList();
23+
var docs = _restaurantsCollection.Find(filter).ToList();
2324

24-
// Deletes documents by using builders
25-
Console.WriteLine("Deleting documents...");
26-
var result = DeleteMultipleRestaurantsBuilder();
25+
// Deletes documents by using builders
26+
Console.WriteLine("Deleting documents...");
27+
var result = DeleteMultipleRestaurantsBuilder();
2728

28-
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
29+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
2930

30-
Restore(docs);
31+
Restore(docs);
32+
33+
// Prints a message if any exceptions occur during the operation
34+
} catch (MongoException me) {
35+
Console.WriteLine("Unable to delete due to an error: " + me);
36+
}
3137
}
3238

3339
// Deletes all documents that have a Borough value of "Brooklyn"

source/includes/code-examples/delete-many/DeleteManyAsync.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@ public class DeleteManyAsync
1414

1515
public static async Task Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
var filter = Builders<Restaurant>.Filter
20-
.Eq(r => r.Borough, "Brooklyn");
20+
var filter = Builders<Restaurant>.Filter
21+
.Eq(r => r.Borough, "Brooklyn");
2122

22-
var docs = _restaurantsCollection.Find(filter).ToList();
23+
var docs = _restaurantsCollection.Find(filter).ToList();
2324

24-
// Deletes documents by using builders
25-
Console.WriteLine("Deleting documents...");
26-
var result = await DeleteMultipleRestaurantsBuilderAsync();
25+
// Deletes documents by using builders
26+
Console.WriteLine("Deleting documents...");
27+
var result = await DeleteMultipleRestaurantsBuilderAsync();
2728

28-
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
29+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
2930

30-
Restore(docs);
31+
Restore(docs);
32+
33+
// Prints a message if any exceptions occur during the operation
34+
} catch (MongoException me) {
35+
Console.WriteLine("Unable to delete due to an error: " + me);
36+
}
3137
}
3238

3339
// Deletes all documents that have a Borough value of "Brooklyn"

source/includes/code-examples/delete-one/DeleteOne.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@ public class DeleteOne
1414

1515
public static void Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
var filter = Builders<Restaurant>.Filter
20-
.Eq(r => r.Name, "Ready Penny Inn");
20+
var filter = Builders<Restaurant>.Filter
21+
.Eq(r => r.Name, "Ready Penny Inn");
22+
23+
var doc = _restaurantsCollection.Find(filter).First();
2124

22-
var doc = _restaurantsCollection.Find(filter).First();
25+
// Deletes a document by using builders
26+
Console.WriteLine("Deleting a document with builders...");
27+
var result = DeleteARestaurantBuilder();
2328

24-
// Deletes a document by using builders
25-
Console.WriteLine("Deleting a document with builders...");
26-
var result = DeleteARestaurantBuilder();
29+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
2730

28-
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
31+
Restore(doc);
2932

30-
Restore(doc);
33+
// Prints a message if any exceptions occur during the operation
34+
} catch (MongoException me) {
35+
Console.WriteLine("Unable to delete due to an error: " + me);
36+
}
3137
}
3238

3339
private static DeleteResult DeleteARestaurantBuilder()

source/includes/code-examples/delete-one/DeleteOneAsync.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,26 @@ public class DeleteOneAsync
1414

1515
public static async Task Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
var filter = Builders<Restaurant>.Filter
20-
.Eq(r => r.Name, "Ready Penny Inn");
20+
var filter = Builders<Restaurant>.Filter
21+
.Eq(r => r.Name, "Ready Penny Inn");
22+
23+
var doc = _restaurantsCollection.Find(filter).First();
2124

22-
var doc = _restaurantsCollection.Find(filter).First();
25+
// Deletes a document by using builders
26+
Console.WriteLine("Deleting a document with builders...");
27+
var result = await DeleteARestaurantBuilderAsync();
2328

24-
// Deletes a document by using builders
25-
Console.WriteLine("Deleting a document with builders...");
26-
var result = await DeleteARestaurantBuilderAsync();
29+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
2730

28-
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
31+
Restore(doc);
2932

30-
Restore(doc);
33+
// Prints a message if any exceptions occur during the operation
34+
} catch (MongoException me) {
35+
Console.WriteLine("Unable to delete due to an error: " + me);
36+
}
3137
}
3238

3339
private static async Task<DeleteResult> DeleteARestaurantBuilderAsync()

source/includes/code-examples/find-many/FindMany.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ public class FindMany
1414

1515
public static void Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
// Finds multiple documents by using builders
20-
Console.WriteLine("Finding documents with builders...:");
21-
var restaurants = FindMultipleRestaurantsBuilderSync();
22-
Console.WriteLine($"Number of documents found: {restaurants.Count}");
20+
// Finds multiple documents by using builders
21+
Console.WriteLine("Finding documents with builders...:");
22+
var restaurants = FindMultipleRestaurantsBuilderSync();
23+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
2324

24-
// Prints extra space for console readability
25-
Console.WriteLine();
25+
// Prints extra space for console readability
26+
Console.WriteLine();
2627

27-
// Retrieves multiple documents by using LINQ
28-
Console.WriteLine("Finding documents with LINQ...:");
29-
restaurants = FindMultipleRestaurantsLinqSync();
30-
Console.WriteLine($"Number of documents found: {restaurants.Count}");
28+
// Retrieves multiple documents by using LINQ
29+
Console.WriteLine("Finding documents with LINQ...:");
30+
restaurants = FindMultipleRestaurantsLinqSync();
31+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
3132

32-
Console.WriteLine();
33+
Console.WriteLine();
3334

34-
// Retrieves all documents in the "restaurants" collection
35-
Console.WriteLine("Finding all documents...:");
36-
restaurants = FindAllRestaurantsSync();
37-
Console.WriteLine($"Number of documents found: {restaurants.Count}");
35+
// Retrieves all documents in the "restaurants" collection
36+
Console.WriteLine("Finding all documents...:");
37+
restaurants = FindAllRestaurantsSync();
38+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
39+
40+
// Prints a message if any exceptions occur during the operation
41+
} catch (MongoException me) {
42+
Console.WriteLine("Unable to find due to an error: " + me);
43+
}
3844
}
3945

4046
public static List<Restaurant> FindMultipleRestaurantsBuilderSync()

source/includes/code-examples/find-many/FindManyAsync.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,33 @@ public class FindManyAsync
1515

1616
public static async Task Main(string[] args)
1717
{
18-
Setup();
18+
try {
19+
Setup();
1920

20-
// Finds multiple documents by using builders
21-
Console.WriteLine("Finding documents with builders...:");
22-
var restaurantsBuilders = await FindMultipleRestaurantsBuilderAsync();
23-
Console.WriteLine($"Number of documents found: {restaurantsBuilders.Count}");
21+
// Finds multiple documents by using builders
22+
Console.WriteLine("Finding documents with builders...:");
23+
var restaurantsBuilders = await FindMultipleRestaurantsBuilderAsync();
24+
Console.WriteLine($"Number of documents found: {restaurantsBuilders.Count}");
2425

25-
// Prints extra space for console readability
26-
Console.WriteLine();
26+
// Prints extra space for console readability
27+
Console.WriteLine();
2728

28-
// Retrieves multiple documents by using LINQ
29-
Console.WriteLine("Finding documents with LINQ...:");
30-
var restaurantsLinq = await FindMultipleRestaurantsLinqAsync();
31-
Console.WriteLine($"Number of documents found: {restaurantsLinq.Count}");
29+
// Retrieves multiple documents by using LINQ
30+
Console.WriteLine("Finding documents with LINQ...:");
31+
var restaurantsLinq = await FindMultipleRestaurantsLinqAsync();
32+
Console.WriteLine($"Number of documents found: {restaurantsLinq.Count}");
3233

33-
Console.WriteLine();
34+
Console.WriteLine();
3435

35-
// Retrieves all documents in the "restaurants" collection
36-
Console.WriteLine("Finding all documents...:");
37-
var allRestaurants = await FindAllRestaurantsAsync();
38-
Console.WriteLine($"Number of documents found: {allRestaurants.Count}");
36+
// Retrieves all documents in the "restaurants" collection
37+
Console.WriteLine("Finding all documents...:");
38+
var allRestaurants = await FindAllRestaurantsAsync();
39+
Console.WriteLine($"Number of documents found: {allRestaurants.Count}");
40+
41+
// Prints a message if any exceptions occur during the operation
42+
} catch (MongoException me) {
43+
Console.WriteLine("Unable to find due to an error: " + me);
44+
}
3945
}
4046

4147
private static async Task<List<Restaurant>> FindMultipleRestaurantsBuilderAsync()

source/includes/code-examples/find-one/FindOne.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ public class FindOne
1414

1515
public static void Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
// Finds one document by using builders
20-
Console.WriteLine("Finding a document with builders...");
21-
FindOneRestaurantBuilder();
20+
// Finds one document by using builders
21+
Console.WriteLine("Finding a document with builders...");
22+
FindOneRestaurantBuilder();
2223

23-
// Prints extra space for console readability
24-
Console.WriteLine();
24+
// Prints extra space for console readability
25+
Console.WriteLine();
2526

26-
// Finds one document by using LINQ
27-
Console.WriteLine("Finding a document with LINQ...");
28-
FindOneRestaurantLinq();
27+
// Finds one document by using LINQ
28+
Console.WriteLine("Finding a document with LINQ...");
29+
FindOneRestaurantLinq();
30+
31+
// Prints a message if any exceptions occur during the operation
32+
} catch (MongoException me) {
33+
Console.WriteLine("Unable to find due to an error: " + me);
34+
}
2935
}
3036

3137
private static void FindOneRestaurantBuilder()

source/includes/code-examples/find-one/FindOneAsync.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ public class FindOneAsync
1515

1616
public static async Task Main(string[] args)
1717
{
18-
Setup();
19-
20-
// Finds one document by using builders
21-
var buildersDocument = await FindOneRestaurantBuilderAsync();
22-
Console.WriteLine("Finding a document with builders...");
23-
Console.WriteLine(buildersDocument.ToBsonDocument());
24-
25-
// Prints extra space for console readability
26-
Console.WriteLine();
27-
28-
// Finds one document by using LINQ
29-
var linqDocument = await FindOneRestaurantLinqAsync();
30-
Console.WriteLine("Finding a document with LINQ...");
31-
Console.WriteLine(linqDocument.ToBsonDocument());
18+
try {
19+
Setup();
20+
21+
// Finds one document by using builders
22+
var buildersDocument = await FindOneRestaurantBuilderAsync();
23+
Console.WriteLine("Finding a document with builders...");
24+
Console.WriteLine(buildersDocument.ToBsonDocument());
25+
26+
// Prints extra space for console readability
27+
Console.WriteLine();
28+
29+
// Finds one document by using LINQ
30+
var linqDocument = await FindOneRestaurantLinqAsync();
31+
Console.WriteLine("Finding a document with LINQ...");
32+
Console.WriteLine(linqDocument.ToBsonDocument());
33+
34+
// Prints a message if any exceptions occur during the operation
35+
} catch (MongoException me) {
36+
Console.WriteLine("Unable to find due to an error: " + me);
37+
}
3238
}
3339

3440
private static async Task<Restaurant> FindOneRestaurantBuilderAsync()

source/includes/code-examples/insert-many/InsertMany.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,38 @@ public class InsertMany
1414

1515
public static void Main(string[] args)
1616
{
17-
Setup();
17+
try {
18+
Setup();
1819

19-
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
20-
var filter = Builders<Restaurant>.Filter
21-
.Eq(r => r.Name, "Mongo's Pizza");
20+
// Creates a filter for all documents that have a "name" value of "Mongo's Pizza"
21+
var filter = Builders<Restaurant>.Filter
22+
.Eq(r => r.Name, "Mongo's Pizza");
2223

23-
// Finds all documents that match the filter
24-
var foundRestaurants = _restaurantsCollection.Find(filter).ToList();
24+
// Finds all documents that match the filter
25+
var foundRestaurants = _restaurantsCollection.Find(filter).ToList();
2526

26-
Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}");
27+
Console.WriteLine($"Number of restaurants found before insert: {foundRestaurants.Count}");
2728

28-
// Prints extra space for console readability
29-
Console.WriteLine();
29+
// Prints extra space for console readability
30+
Console.WriteLine();
3031

31-
Console.WriteLine("Inserting documents...");
32+
Console.WriteLine("Inserting documents...");
3233

33-
// Inserts the documents by using a helper method
34-
InsertManyRestaurants();
34+
// Inserts the documents by using a helper method
35+
InsertManyRestaurants();
3536

36-
// Finds all documents that match the filter after the insert
37-
foundRestaurants = _restaurantsCollection.Find(filter).ToList();
37+
// Finds all documents that match the filter after the insert
38+
foundRestaurants = _restaurantsCollection.Find(filter).ToList();
3839

39-
// Prints the number of documents found
40-
Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}");
40+
// Prints the number of documents found
41+
Console.WriteLine($"Number of restaurants inserted: {foundRestaurants.Count}");
4142

42-
Cleanup();
43+
Cleanup();
44+
45+
// Prints a message if any exceptions occur during the operation
46+
} catch (MongoException me) {
47+
Console.WriteLine("Unable to insert due to an error: " + me);
48+
}
4349
}
4450

4551
private static void InsertManyRestaurants()

0 commit comments

Comments
 (0)