Skip to content

Commit 6ba46ac

Browse files
DOCSP-24529 Adding Tabs (#7)
1 parent aa9b341 commit 6ba46ac

File tree

4 files changed

+151
-26
lines changed

4 files changed

+151
-26
lines changed

snooty.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ driver-long = "MongoDB .NET/C# Driver"
1818
driver-short = ".NET/C# Driver"
1919
lang-framework = ".NET/C#"
2020
language = "C#"
21-
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
22-
version-number = "2.17" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
23-
version = "v{+version-number+}"
24-
21+
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
22+
version-number = "2.17" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
23+
version = "v{+version-number+}"
2524
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/usage-examples/code-snippets"
2625
stable-api = "Stable API"
2726
api-root = "https://mongodb.github.io/mongo-csharp-driver/{+version-number+}/apidocs/html"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
7+
namespace CSharpExamples.UsageExamples;
8+
9+
public class FindOneAsync
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 one document using builders
19+
var buildersDocument = FindOneRestaurantBuilderAsync().Result.ToBsonDocument();
20+
Console.WriteLine("Finding a document with builders...");
21+
Console.WriteLine(buildersDocument);
22+
23+
// Extra space for console readability
24+
Console.WriteLine();
25+
26+
// Find one document using LINQ
27+
var linqDocument = FindOneRestaurantLINQAsync().Result.ToBsonDocument();
28+
Console.WriteLine("Finding a document with LINQ...");
29+
Console.WriteLine(linqDocument);
30+
}
31+
32+
public static async Task<Restaurant> FindOneRestaurantBuilderAsync()
33+
{
34+
// start-find-builders
35+
var filter = Builders<Restaurant>.Filter
36+
.Eq("name", "Bagels N Buns");
37+
38+
return await _restaurantsCollection.Find(filter).FirstAsync();
39+
// end-find-builders
40+
41+
}
42+
43+
public static async Task<Restaurant> FindOneRestaurantLINQAsync()
44+
{
45+
// start-find-linq
46+
return await _restaurantsCollection.AsQueryable()
47+
.Where(r => r.Name == "Bagels N Buns").FirstAsync();
48+
// end-find-linq
49+
50+
}
51+
52+
public static void Setup()
53+
{
54+
// This allows automapping of the camelCase database fields to our models.
55+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
56+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
57+
58+
// Establish the connection to MongoDB and get the restaurants database
59+
var uri = _mongoConnectionString;
60+
var mongoClient = new MongoClient(uri);
61+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
62+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
63+
}
64+
}
65+
66+
// start-model
67+
public class Restaurant
68+
{
69+
public ObjectId Id { get; set; }
70+
71+
public string Name { get; set; }
72+
73+
[BsonElement("restaurant_id")]
74+
public string RestaurantId { get; set; }
75+
76+
public string Cuisine { get; set; }
77+
78+
public object Address { get; set; }
79+
80+
public string Borough { get; set; }
81+
82+
public List<object> Grades { get; set; }
83+
}
84+
// end-model

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ public static void Main(string[] args)
1515
Setup();
1616

1717
// Find one document using builders
18+
Console.WriteLine("Finding a document with builders...");
1819
FindOneRestaurantBuilder();
1920

2021
// Extra space for console readability
2122
Console.WriteLine();
2223

2324
// Find one document using LINQ
25+
Console.WriteLine("Finding a document with LINQ...");
2426
FindOneRestaurantLINQ();
2527
}
2628

source/usage-examples/findOne.txt

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,78 @@ Examples
2020

2121
These examples use the following ``Restaurant`` class as a model:
2222

23-
.. literalinclude:: ../includes/code-examples/Find.cs
23+
.. literalinclude:: ../includes/code-examples/FindOneSync.cs
2424
:start-after: start-model
2525
:end-before: end-model
2626
:language: csharp
2727
:copyable:
2828
:dedent:
2929

30-
Find a Document Using Builders
31-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30+
.. tabs::
3231

33-
The following example uses builders to find a document in the ``restaurants``
34-
collection with the ``name`` "Bagels N Buns".
32+
.. tab:: Builders Async
33+
:tabid: builders-async
3534

36-
.. literalinclude:: ../includes/code-examples/Find.cs
37-
:start-after: start-find-builders
38-
:end-before: end-find-builders
39-
:language: csharp
40-
:copyable:
41-
:dedent:
35+
Find a Document Using Builders
36+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4237

43-
Find a Document Using LINQ
44-
~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
The following example uses builders to asynchronously find a document in
39+
the ``restaurants`` collection with the ``name`` "Bagels N Buns".
4540

46-
The following example uses LINQ to find a document in the ``restaurants``
47-
collection with the ``name`` "Bagels N Buns".
41+
.. literalinclude:: ../includes/code-examples/FindOneAsync.cs
42+
:start-after: start-find-builders
43+
:end-before: end-find-builders
44+
:language: csharp
45+
:copyable:
46+
:dedent:
4847

49-
.. literalinclude:: ../includes/code-examples/Find.cs
50-
:start-after: start-find-linq
51-
:end-before: end-find-linq
52-
:language: csharp
53-
:copyable:
54-
:dedent:
48+
.. tab:: Builders Synchronous
49+
:tabid: builders-sync
50+
51+
Find a Document Using Builders
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
54+
The following example uses builders to synchronously find a document in the ``restaurants``
55+
collection with the ``name`` "Bagels N Buns".
56+
57+
.. literalinclude:: ../includes/code-examples/FindOneSync.cs
58+
:start-after: start-find-builders
59+
:end-before: end-find-builders
60+
:language: csharp
61+
:copyable:
62+
:dedent:
63+
64+
.. tab:: LINQ Async
65+
:tabid: linq-async
66+
67+
Find a Document Using LINQ
68+
~~~~~~~~~~~~~~~~~~~~~~~~~~
69+
70+
The following example uses LINQ to asynchronously find a document in the ``restaurants``
71+
collection with the ``name`` "Bagels N Buns".
72+
73+
.. literalinclude:: ../includes/code-examples/FindOneAsync.cs
74+
:start-after: start-find-linq
75+
:end-before: end-find-linq
76+
:language: csharp
77+
:copyable:
78+
:dedent:
79+
80+
.. tab:: LINQ Synchronous
81+
:tabid: linq-sync
82+
83+
Find a Document Using LINQ
84+
~~~~~~~~~~~~~~~~~~~~~~~~~~
85+
86+
The following example uses LINQ to synchronously find a document in the ``restaurants``
87+
collection with the ``name`` "Bagels N Buns".
88+
89+
.. literalinclude:: ../includes/code-examples/FindOneSync.cs
90+
:start-after: start-find-linq
91+
:end-before: end-find-linq
92+
:language: csharp
93+
:copyable:
94+
:dedent:
5595

5696
Expected Result
5797
~~~~~~~~~~~~~~~
@@ -89,4 +129,4 @@ To learn more about LINQ, see <LINQ page>.
89129
API Documentation
90130
~~~~~~~~~~~~~~~~~
91131

92-
`Find() <{+api+}/html/Overload_MongoDB_Driver_IMongoCollectionExtensions_Find.htm>`__
132+
`Find() <{+api-root+}/html/Overload_MongoDB_Driver_IMongoCollectionExtensions_Find.htm>`__

0 commit comments

Comments
 (0)