Skip to content

Commit ff7972e

Browse files
DOCSP-24530 Find Multiple Usage Example (#8)
Co-authored-by: Mike Woofter <[email protected]>
1 parent 6ba46ac commit ff7972e

File tree

6 files changed

+396
-23
lines changed

6 files changed

+396
-23
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using MongoDB.Bson.Serialization.Conventions;
4+
using MongoDB.Driver;
5+
using static System.Console;
6+
7+
namespace CSharpExamples.UsageExamples;
8+
9+
public class FindMany
10+
{
11+
private static IMongoCollection<Restaurant> _restaurantsCollection;
12+
private static string _mongoConnectionString = "<Your MongoDB URI>";
13+
14+
public static void Main(string[] args)
15+
{
16+
Setup();
17+
18+
// Find multiple documents using builders
19+
WriteLine("Finding documents with builders...:");
20+
FindMultipleRestaurantsBuilderSync();
21+
22+
// Extra space for console readability
23+
WriteLine();
24+
25+
// Find multiple documents using LINQ
26+
WriteLine("Finding documents with LINQ...:");
27+
FindMultipleRestaurantsLINQSync();
28+
29+
WriteLine();
30+
31+
// Find All restaurants
32+
WriteLine("Finding all documents...:");
33+
FindAllRestaurantsSync();
34+
}
35+
36+
private static void FindMultipleRestaurantsBuilderSync()
37+
{
38+
// start-find-builders-sync
39+
var filter = Builders<Restaurant>.Filter
40+
.Eq("cuisine", "Pizza");
41+
42+
var restaurants = _restaurantsCollection.Find(filter).ToList();
43+
// end-find-builders-sync
44+
45+
WriteLine("Number of documents found: " + restaurants.Count);
46+
}
47+
48+
private static void FindMultipleRestaurantsLINQSync()
49+
{
50+
// start-find-linq-sync
51+
var query = _restaurantsCollection.AsQueryable()
52+
.Where(r => r.Cuisine == "Pizza").ToList();
53+
// end-find-linq-sync
54+
55+
WriteLine("Number of documents found: " + query.Count);
56+
}
57+
58+
private static void FindAllRestaurantsSync()
59+
{
60+
// start-find-all-sync
61+
var restaurants = _restaurantsCollection.Find(new BsonDocument()).ToList();
62+
// end-find-all-sync
63+
WriteLine("Number of documents found: " + restaurants.Count);
64+
}
65+
66+
private static void Setup()
67+
{
68+
// This allows automapping of the camelCase database fields to our models.
69+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
70+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
71+
72+
// Establish the connection to MongoDB and get the restaurants database
73+
var mongoClient = new MongoClient(_mongoConnectionString);
74+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
75+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
76+
}
77+
}
78+
79+
// start-model
80+
public class Restaurant
81+
{
82+
public ObjectId Id { get; set; }
83+
84+
public string Name { get; set; }
85+
86+
[BsonElement("restaurant_id")]
87+
public string RestaurantId { get; set; }
88+
89+
public string Cuisine { get; set; }
90+
91+
public object Address { get; set; }
92+
93+
public string Borough { get; set; }
94+
95+
public List<object> Grades { get; set; }
96+
}
97+
// end-model
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using MongoDB.Bson.Serialization.Conventions;
4+
using MongoDB.Driver;
5+
using MongoDB.Driver.Linq;
6+
using static System.Console;
7+
8+
namespace CSharpExamples.UsageExamples;
9+
10+
public class FindManyAsync
11+
{
12+
private static IMongoCollection<Restaurant> _restaurantsCollection;
13+
private static string _mongoConnectionString = "<Your MongoDB URI>";
14+
15+
public static void Main(string[] args)
16+
{
17+
Setup();
18+
19+
// Find multiple documents using builders
20+
WriteLine("Finding documents with builders...:");
21+
var restaurantsBuilders = FindMultipleRestaurantsBuilderAsync();
22+
WriteLine("Number of documents found: " + restaurantsBuilders.Result.Count);
23+
24+
// Extra space for console readability
25+
WriteLine();
26+
27+
// Find multiple documents using LINQ
28+
WriteLine("Finding documents with LINQ...:");
29+
var restaurantsLINQ = FindMultipleRestaurantsLINQAsync();
30+
WriteLine("Number of documents found: " + restaurantsLINQ.Result.Count);
31+
32+
WriteLine();
33+
34+
// Find all documents
35+
WriteLine("Finding all documents...:");
36+
var allRestaurants = FindAllRestaurantsAsync();
37+
WriteLine("Number of documents found: " + allRestaurants.Result.Count);
38+
}
39+
40+
private static async Task<List<Restaurant>> FindMultipleRestaurantsBuilderAsync()
41+
{
42+
// start-find-builders-async
43+
var filter = Builders<Restaurant>.Filter
44+
.Eq("cuisine", "Pizza");
45+
46+
return await _restaurantsCollection.Find(filter).ToListAsync();
47+
// end-find-builders-async
48+
}
49+
50+
private static async Task<List<Restaurant>> FindMultipleRestaurantsLINQAsync()
51+
{
52+
// start-find-linq-async
53+
return await _restaurantsCollection.AsQueryable()
54+
.Where(r => r.Cuisine == "Pizza").ToListAsync();
55+
// end-find-linq-async
56+
57+
}
58+
59+
private static async Task<List<Restaurant>> FindAllRestaurantsAsync()
60+
{
61+
// start-find-all-async
62+
return await _restaurantsCollection.Find(new BsonDocument()).ToListAsync();
63+
// end-find-all-async
64+
}
65+
66+
private static void Setup()
67+
{
68+
// This allows automapping of the camelCase database fields to our models.
69+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
70+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
71+
72+
// Establish the connection to MongoDB and get the restaurants database
73+
var mongoClient = new MongoClient(_mongoConnectionString);
74+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
75+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
76+
}
77+
}
78+
79+
// start-model
80+
public class Restaurant
81+
{
82+
public ObjectId Id { get; set; }
83+
84+
public string Name { get; set; }
85+
86+
[BsonElement("restaurant_id")]
87+
public string RestaurantId { get; set; }
88+
89+
public string Cuisine { get; set; }
90+
91+
public object Address { get; set; }
92+
93+
public string Borough { get; set; }
94+
95+
public List<object> Grades { get; set; }
96+
}
97+
// end-model

source/includes/code-examples/FindOneSync.cs renamed to source/includes/code-examples/FindOne.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MongoDB.Bson.Serialization.Attributes;
33
using MongoDB.Bson.Serialization.Conventions;
44
using MongoDB.Driver;
5+
using static System.Console;
56

67
namespace CSharpExamples.UsageExamples;
78

@@ -15,18 +16,18 @@ public static void Main(string[] args)
1516
Setup();
1617

1718
// Find one document using builders
18-
Console.WriteLine("Finding a document with builders...");
19+
WriteLine("Finding a document with builders...");
1920
FindOneRestaurantBuilder();
2021

2122
// Extra space for console readability
22-
Console.WriteLine();
23+
WriteLine();
2324

2425
// Find one document using LINQ
25-
Console.WriteLine("Finding a document with LINQ...");
26+
WriteLine("Finding a document with LINQ...");
2627
FindOneRestaurantLINQ();
2728
}
2829

29-
public static void FindOneRestaurantBuilder()
30+
private static void FindOneRestaurantBuilder()
3031
{
3132
// start-find-builders
3233
var filter = Builders<Restaurant>.Filter
@@ -35,22 +36,22 @@ public static void FindOneRestaurantBuilder()
3536
var restaurant = _restaurantsCollection.Find(filter).First();
3637
// end-find-builders
3738

38-
Console.WriteLine(restaurant.ToBsonDocument());
39+
WriteLine(restaurant.ToBsonDocument());
3940

4041
}
4142

42-
public static void FindOneRestaurantLINQ()
43+
private static void FindOneRestaurantLINQ()
4344
{
4445
// start-find-linq
4546
var query = _restaurantsCollection.AsQueryable()
4647
.Where(r => r.Name == "Bagels N Buns");
4748
// end-find-linq
4849

49-
Console.WriteLine(query.ToBsonDocument());
50+
WriteLine(query.ToBsonDocument());
5051

5152
}
5253

53-
public static void Setup()
54+
private static void Setup()
5455
{
5556
// This allows automapping of the camelCase database fields to our models.
5657
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };

source/includes/code-examples/FindOneAsync.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using MongoDB.Bson.Serialization.Conventions;
44
using MongoDB.Driver;
55
using MongoDB.Driver.Linq;
6+
using static System.Console;
67

78
namespace CSharpExamples.UsageExamples;
89

@@ -17,19 +18,19 @@ public static void Main(string[] args)
1718

1819
// Find one document using builders
1920
var buildersDocument = FindOneRestaurantBuilderAsync().Result.ToBsonDocument();
20-
Console.WriteLine("Finding a document with builders...");
21-
Console.WriteLine(buildersDocument);
21+
WriteLine("Finding a document with builders...");
22+
WriteLine(buildersDocument);
2223

2324
// Extra space for console readability
24-
Console.WriteLine();
25+
WriteLine();
2526

2627
// Find one document using LINQ
2728
var linqDocument = FindOneRestaurantLINQAsync().Result.ToBsonDocument();
28-
Console.WriteLine("Finding a document with LINQ...");
29-
Console.WriteLine(linqDocument);
29+
WriteLine("Finding a document with LINQ...");
30+
WriteLine(linqDocument);
3031
}
3132

32-
public static async Task<Restaurant> FindOneRestaurantBuilderAsync()
33+
private static async Task<Restaurant> FindOneRestaurantBuilderAsync()
3334
{
3435
// start-find-builders
3536
var filter = Builders<Restaurant>.Filter
@@ -40,7 +41,7 @@ public static async Task<Restaurant> FindOneRestaurantBuilderAsync()
4041

4142
}
4243

43-
public static async Task<Restaurant> FindOneRestaurantLINQAsync()
44+
private static async Task<Restaurant> FindOneRestaurantLINQAsync()
4445
{
4546
// start-find-linq
4647
return await _restaurantsCollection.AsQueryable()
@@ -49,15 +50,14 @@ public static async Task<Restaurant> FindOneRestaurantLINQAsync()
4950

5051
}
5152

52-
public static void Setup()
53+
private static void Setup()
5354
{
5455
// This allows automapping of the camelCase database fields to our models.
5556
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
5657
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
5758

5859
// Establish the connection to MongoDB and get the restaurants database
59-
var uri = _mongoConnectionString;
60-
var mongoClient = new MongoClient(uri);
60+
var mongoClient = new MongoClient(_mongoConnectionString);
6161
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
6262
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
6363
}

0 commit comments

Comments
 (0)