Skip to content

Commit f5b9888

Browse files
Usage Examples (#51)
* usage examples * DL feedback * make Main async * DL feedback 2 * DL feedback 3 * fix literal include * DL feedback 4 * DL feedback 5 * BD feedback * fix includes * DL feedback 6 * DL feedback 7 * compilation errors * DL feedback 8 Co-authored-by: Mike Woofter <[email protected]>
1 parent 9b87e3d commit f5b9888

36 files changed

+2427
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using MongoDB.Bson.Serialization.Conventions;
2+
using MongoDB.Driver;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteMany;
5+
6+
public class DeleteMany
7+
{
8+
private static IMongoCollection<Restaurant> _restaurantsCollection;
9+
private const string MongoConnectionString = "<Your MongoDB URI>";
10+
11+
public static void Main(string[] args)
12+
{
13+
Setup();
14+
15+
var filter = Builders<Restaurant>.Filter
16+
.Eq(r => r.Borough, "Brooklyn");
17+
18+
var docs = _restaurantsCollection.Find(filter).ToList();
19+
20+
// Deleting documents using builders
21+
Console.WriteLine("Deleting documents...");
22+
var result = DeleteMultipleRestaurantsBuilder();
23+
24+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
25+
26+
Restore(docs);
27+
}
28+
29+
private static DeleteResult DeleteMultipleRestaurantsBuilder()
30+
{
31+
// start-delete-many-builders
32+
var filter = Builders<Restaurant>.Filter
33+
.Eq(r => r.Borough, "Brooklyn");
34+
35+
return _restaurantsCollection.DeleteMany(filter);
36+
// end-delete-many-builders
37+
}
38+
39+
private static void Restore(IEnumerable<Restaurant> docs)
40+
{
41+
_restaurantsCollection.InsertMany(docs);
42+
Console.WriteLine("Resetting sample data...done.");
43+
}
44+
45+
private static void Setup()
46+
{
47+
// This allows automapping of the camelCase database fields to our models.
48+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
49+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
50+
51+
// Establish the connection to MongoDB and get the restaurants database
52+
var mongoClient = new MongoClient(MongoConnectionString);
53+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
54+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
55+
}
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using MongoDB.Bson.Serialization.Conventions;
2+
using MongoDB.Driver;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteMany;
5+
6+
public class DeleteManyAsync
7+
{
8+
private static IMongoCollection<Restaurant> _restaurantsCollection;
9+
private const string MongoConnectionString = "<Your MongoDB URI>";
10+
11+
public static async Task Main(string[] args)
12+
{
13+
Setup();
14+
15+
var filter = Builders<Restaurant>.Filter
16+
.Eq(r => r.Borough, "Brooklyn");
17+
18+
var docs = _restaurantsCollection.Find(filter).ToList();
19+
20+
// Deleting documents using builders
21+
Console.WriteLine("Deleting documents...");
22+
var result = await DeleteMultipleRestaurantsBuilderAsync();
23+
24+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
25+
26+
Restore(docs);
27+
28+
return result;
29+
}
30+
31+
private static async Task<DeleteResult> DeleteMultipleRestaurantsBuilderAsync()
32+
{
33+
// start-delete-many-async
34+
var filter = Builders<Restaurant>.Filter
35+
.Eq(r => r.Borough, "Brooklyn");
36+
37+
return await _restaurantsCollection.DeleteManyAsync(filter);
38+
// end-delete-many-async
39+
}
40+
41+
private static void Restore(IEnumerable<Restaurant> docs)
42+
{
43+
_restaurantsCollection.InsertMany(docs);
44+
Console.WriteLine("Resetting sample data...done.");
45+
}
46+
47+
private static void Setup()
48+
{
49+
// This allows automapping of the camelCase database fields to our models.
50+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
51+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
52+
53+
// Establish the connection to MongoDB and get the restaurants database
54+
var mongoClient = new MongoClient(MongoConnectionString);
55+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
56+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteMany;
5+
6+
// start-model
7+
public class Restaurant
8+
{
9+
public ObjectId Id { get; set; }
10+
11+
public string Name { get; set; }
12+
13+
[BsonElement("restaurant_id")]
14+
public string RestaurantId { get; set; }
15+
16+
public string Cuisine { get; set; }
17+
18+
public object Address { get; set; }
19+
20+
public string Borough { get; set; }
21+
22+
public List<object> Grades { get; set; }
23+
}
24+
// end-model
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using MongoDB.Bson.Serialization.Conventions;
2+
using MongoDB.Driver;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteOne;
5+
6+
public class DeleteOne
7+
{
8+
private static IMongoCollection<Restaurant> _restaurantsCollection;
9+
private const string MongoConnectionString = "<Your MongoDB URI>";
10+
11+
public static void Main(string[] args)
12+
{
13+
Setup();
14+
15+
var filter = Builders<Restaurant>.Filter
16+
.Eq(r => r.Name, "Ready Penny Inn");
17+
18+
var doc = _restaurantsCollection.Find(filter).First();
19+
20+
// Delete a document using builders
21+
Console.WriteLine("Deleting a document with builders...");
22+
var result = DeleteARestaurantBuilder();
23+
24+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
25+
26+
Restore(doc);
27+
}
28+
29+
private static DeleteResult DeleteARestaurantBuilder()
30+
{
31+
// start-delete-one-builders
32+
var filter = Builders<Restaurant>.Filter
33+
.Eq(r => r.Name, "Ready Penny Inn");
34+
35+
return _restaurantsCollection.DeleteOne(filter);
36+
// end-delete-one-builders
37+
}
38+
39+
private static void Restore(Restaurant doc)
40+
{
41+
_restaurantsCollection.InsertOne(doc);
42+
}
43+
44+
private static void Setup()
45+
{
46+
// This allows automapping of the camelCase database fields to our models.
47+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
48+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
49+
50+
// Establish the connection to MongoDB and get the restaurants database
51+
var mongoClient = new MongoClient(MongoConnectionString);
52+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
53+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using MongoDB.Bson.Serialization.Conventions;
2+
using MongoDB.Driver;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteOne;
5+
6+
public class DeleteOneAsync
7+
{
8+
private static IMongoCollection<Restaurant> _restaurantsCollection;
9+
private const string MongoConnectionString = "<Your MongoDB URI>";
10+
11+
public static async Task Main(string[] args)
12+
{
13+
Setup();
14+
15+
var filter = Builders<Restaurant>.Filter
16+
.Eq(r => r.Name, "Ready Penny Inn");
17+
18+
var doc = _restaurantsCollection.Find(filter).First();
19+
20+
// Delete a document using builders
21+
Console.WriteLine("Deleting a document with builders...");
22+
var result = await DeleteARestaurantBuilderAsync();
23+
24+
Console.WriteLine($"Deleted documents: {result.DeletedCount}");
25+
26+
Restore(doc);
27+
}
28+
29+
private static async Task<DeleteResult> DeleteARestaurantBuilderAsync()
30+
{
31+
// start-delete-one-builders-async
32+
var filter = Builders<Restaurant>.Filter
33+
.Eq(r => r.Name, "Ready Penny Inn");
34+
35+
return await _restaurantsCollection.DeleteOneAsync(filter);
36+
// end-delete-one-builders-async
37+
}
38+
39+
private static void Restore(Restaurant doc)
40+
{
41+
_restaurantsCollection.InsertOne(doc);
42+
}
43+
44+
private static void Setup()
45+
{
46+
// This allows automapping of the camelCase database fields to our models.
47+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
48+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
49+
50+
// Establish the connection to MongoDB and get the restaurants database
51+
var mongoClient = new MongoClient(MongoConnectionString);
52+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
53+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
54+
}
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
4+
namespace CSharpExamples.UsageExamples.DeleteOne;
5+
6+
// start-model
7+
public class Restaurant
8+
{
9+
public ObjectId Id { get; set; }
10+
11+
public string Name { get; set; }
12+
13+
[BsonElement("restaurant_id")]
14+
public string RestaurantId { get; set; }
15+
16+
public string Cuisine { get; set; }
17+
18+
public object Address { get; set; }
19+
20+
public string Borough { get; set; }
21+
22+
public List<object> Grades { get; set; }
23+
}
24+
// end-model
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using MongoDB.Bson.Serialization.Conventions;
2+
using MongoDB.Driver;
3+
4+
namespace CSharpExamples.UsageExamples.FindMany;
5+
6+
public class FindMany
7+
{
8+
private static IMongoCollection<Restaurant> _restaurantsCollection;
9+
private const string MongoConnectionString = "<Your MongoDB URI>";
10+
11+
public static void Main(string[] args)
12+
{
13+
Setup();
14+
15+
// Find multiple documents using builders
16+
Console.WriteLine("Finding documents with builders...:");
17+
var restaurants = FindMultipleRestaurantsBuilderSync();
18+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
19+
20+
// Extra space for console readability
21+
Console.WriteLine();
22+
23+
// Find multiple documents using LINQ
24+
Console.WriteLine("Finding documents with LINQ...:");
25+
restaurants = FindMultipleRestaurantsLinqSync();
26+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
27+
28+
Console.WriteLine();
29+
30+
// Find all restaurants
31+
Console.WriteLine("Finding all documents...:");
32+
restaurants = FindAllRestaurantsSync();
33+
Console.WriteLine($"Number of documents found: {restaurants.Count}");
34+
}
35+
36+
public static List<Restaurant> FindMultipleRestaurantsBuilderSync()
37+
{
38+
// start-find-builders-sync
39+
var filter = Builders<Restaurant>.Filter
40+
.Eq("cuisine", "Pizza");
41+
42+
return _restaurantsCollection.Find(filter).ToList();
43+
// end-find-builders-sync
44+
}
45+
46+
public static List<Restaurant> FindMultipleRestaurantsLinqSync()
47+
{
48+
// start-find-linq-sync
49+
return _restaurantsCollection.AsQueryable()
50+
.Where(r => r.Cuisine == "Pizza").ToList();
51+
// end-find-linq-sync
52+
}
53+
54+
private static List<Restaurant> FindAllRestaurantsSync()
55+
{
56+
// start-find-all-sync
57+
var filter = Builders<Restaurant>.Filter.Empty;
58+
59+
return _restaurantsCollection.Find(filter)
60+
.ToList();
61+
// end-find-all-sync
62+
}
63+
64+
private static void Setup()
65+
{
66+
// This allows automapping of the camelCase database fields to our models.
67+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
68+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
69+
70+
// Establish the connection to MongoDB and get the restaurants database
71+
var mongoClient = new MongoClient(MongoConnectionString);
72+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
73+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
74+
}
75+
}

0 commit comments

Comments
 (0)