Skip to content

Commit 2a4c70f

Browse files
Find Usage Example (#4)
1 parent 21a2fc3 commit 2a4c70f

File tree

4 files changed

+193
-7
lines changed

4 files changed

+193
-7
lines changed

snooty.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name = "csharp"
22
title = "C#/.NET"
33
toc_landing_pages = [
4-
"/fundamentals/connection",
5-
"/fundamentals/crud",
6-
"/usage-examples"
4+
"/fundamentals/connection",
5+
"/fundamentals/crud",
6+
"/usage-examples",
77
]
88

99
intersphinx = [
1010
"https://www.mongodb.com/docs/manual/objects.inv",
1111
"https://www.mongodb.com/docs/drivers/objects.inv",
12-
"https://www.mongodb.com/docs/atlas/objects.inv"
12+
"https://www.mongodb.com/docs/atlas/objects.inv",
1313
]
1414
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
1515

@@ -18,7 +18,8 @@ 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 = "v2.17" # always set this to the driver branch (i.e. v1.7.0, v1.8.0, etc.)
23-
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/usage-examples/code-snippets"
21+
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
22+
version = "v2.17" # always set this to the driver branch (i.e. v1.7.0, v1.8.0, etc.)
23+
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/code-examples/"
2424
stable-api = "Stable API"
25+
api = "https://mongodb.github.io/mongo-csharp-driver/{+version+}/apidocs"

source/includes/code-examples/Find.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using MongoDB.Bson.Serialization.Conventions;
4+
using MongoDB.Driver;
5+
6+
namespace CSharpExamples.UsageExamples;
7+
8+
public class FindOne
9+
{
10+
private static IMongoCollection<Restaurant> _restaurantsCollection;
11+
private static string _mongoConnectionString = "<Your MongoDB URI>";
12+
13+
public static void Main(string[] args)
14+
{
15+
Setup();
16+
17+
// Find one document using builders
18+
FindOneRestaurantBuilder();
19+
20+
// Extra space for console readability
21+
Console.WriteLine();
22+
23+
// Find one document using LINQ
24+
FindOneRestaurantLINQ();
25+
}
26+
27+
public static void FindOneRestaurantBuilder()
28+
{
29+
// start-find-builders
30+
var filter = Builders<Restaurant>.Filter
31+
.Eq("name", "Bagels N Buns");
32+
33+
var restaurant = _restaurantsCollection.Find(filter).First();
34+
// end-find-builders
35+
36+
Console.WriteLine(restaurant.ToBsonDocument());
37+
38+
}
39+
40+
public static void FindOneRestaurantLINQ()
41+
{
42+
// start-find-linq
43+
var query = _restaurantsCollection.AsQueryable()
44+
.Where(r => r.Name == "Bagels N Buns");
45+
// end-find-linq
46+
47+
Console.WriteLine(query.ToBsonDocument());
48+
49+
}
50+
51+
public static void Setup()
52+
{
53+
// This allows automapping of the camelCase database fields to our models.
54+
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
55+
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
56+
57+
// Establish the connection to MongoDB and get the restaurants database
58+
var mongoClient = new MongoClient(_mongoConnectionString);
59+
var restaurantsDatabase = mongoClient.GetDatabase("sample_restaurants");
60+
_restaurantsCollection = restaurantsDatabase.GetCollection<Restaurant>("restaurants");
61+
}
62+
}
63+
64+
// start-model
65+
public class Restaurant
66+
{
67+
public ObjectId Id { get; set; }
68+
69+
public string Name { get; set; }
70+
71+
[BsonElement("restaurant_id")]
72+
public string RestaurantId { get; set; }
73+
74+
public string Cuisine { get; set; }
75+
76+
public object Address { get; set; }
77+
78+
public string Borough { get; set; }
79+
80+
public List<object> Grades { get; set; }
81+
}
82+
// end-model

source/usage-examples.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. _csharp-usage-examples:
2+
3+
==============
4+
Usage Examples
5+
==============
6+
7+
TODO: Write usage examples page
8+
9+
.. toctree::
10+
11+
/usage-examples/findOne.txt

source/usage-examples/findOne.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
.. _csharp-find-one:
2+
3+
===============
4+
Find a Document
5+
===============
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
You can retrieve a single document from a collection by using the ``Find()``
16+
method.
17+
18+
Examples
19+
--------
20+
21+
These examples use the following ``Restaurant`` class as a model:
22+
23+
.. literalinclude:: ../includes/code-examples/Find.cs
24+
:start-after: start-model
25+
:end-before: end-model
26+
:language: csharp
27+
:copyable:
28+
:dedent:
29+
30+
Find a Document Using Builders
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
33+
The following example uses builders to find a document in the ``restaurants``
34+
collection with the ``name`` "Bagels N Buns".
35+
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:
42+
43+
Find a Document Using LINQ
44+
~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
46+
The following example uses LINQ to find a document in the ``restaurants``
47+
collection with the ``name`` "Bagels N Buns".
48+
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:
55+
56+
Expected Result
57+
~~~~~~~~~~~~~~~
58+
59+
After running the :ref:`full example <find-runnable-example>`, each ``Find()`` method
60+
returns a result containing the following document:
61+
62+
.. code-block:: none
63+
64+
// results truncated
65+
{
66+
"_id" : ObjectId("5eb3d668b31de5d588f42950"),
67+
"name" : "Bagels N Buns",
68+
"restaurant_id" : "40363427",
69+
"cuisine" : "Delicatessen",
70+
"address" : {...},
71+
"borough" : "Staten Island",
72+
"grades" : [...]
73+
}
74+
75+
Additional Information
76+
----------------------
77+
78+
.. _find-runnable-example:
79+
80+
For a fully runnable example of the ``Find`` operations on this page, see the
81+
`Find Example <{+example+}/Find.cs>`__
82+
83+
.. TODO: Add links to references once pages are done
84+
85+
To learn more about builders, see <builders page>.
86+
87+
To learn more about LINQ, see <LINQ page>.
88+
89+
API Documentation
90+
~~~~~~~~~~~~~~~~~
91+
92+
`Find() <{+api+}/html/Overload_MongoDB_Driver_IMongoCollectionExtensions_Find.htm>`__

0 commit comments

Comments
 (0)