Skip to content

Commit 757d33f

Browse files
authored
Merge pull request #1 from mongoKart/docsp-32163-quick-start
2 parents 4010f98 + 1dd6160 commit 757d33f

File tree

6 files changed

+229
-7
lines changed

6 files changed

+229
-7
lines changed

snooty.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ framework-version = "7.0"
2222
language = "C#"
2323
mongo-community = "MongoDB Community Edition"
2424
mongo-enterprise = "MongoDB Enterprise Edition"
25-
docs-branch = "main" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
26-
version-number = "7.0" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
25+
docs-branch = "main" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
26+
version-number = "7.0" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
2727
version = "v{+version-number+}"
2828
stable-api = "Stable API"
2929
bool-data-type = "``boolean``"
3030
string-data-type = "``string``"
3131
int-data-type = "``integer``"
3232
not-available = "N/A"
33+
package = "MongoDB.EntityFrameworkCore"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using MongoDB.Bson;
3+
using MongoDB.Driver;
4+
using MongoDB.EntityFrameworkCore.Extensions;
5+
6+
var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI");
7+
8+
if (connectionString == null)
9+
{
10+
Console.WriteLine("You must set your 'MONGODB_URI' environment variable. To learn how to set it, see https://www.mongodb.com/docs/drivers/csharp/current/quick-start/#set-your-connection-string");
11+
Environment.Exit(0);
12+
}
13+
var client = new MongoClient(connectionString);
14+
15+
var db = MflixDbContext.Create(client.GetDatabase("sample_mflix"));
16+
17+
var movie = db.Movies.First(m => m.title == "Back to the Future");
18+
Console.WriteLine(movie.plot);
19+
20+
internal class MflixDbContext : DbContext
21+
{
22+
public DbSet<Movie> Movies { get; init; }
23+
24+
public static MflixDbContext Create(IMongoDatabase database) =>
25+
new(new DbContextOptionsBuilder<MflixDbContext>()
26+
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
27+
.Options);
28+
29+
public MflixDbContext(DbContextOptions options)
30+
: base(options)
31+
{
32+
}
33+
34+
protected override void OnModelCreating(ModelBuilder modelBuilder)
35+
{
36+
base.OnModelCreating(modelBuilder);
37+
38+
modelBuilder.Entity<Movie>().ToCollection("movies");
39+
}
40+
}
41+
42+
internal class Movie
43+
{
44+
public ObjectId _id { get; set; }
45+
public string title { get; set; }
46+
public string rated { get; set; }
47+
public string plot { get; set; }
48+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
This guide shows you how to create a .NET application that uses the {+provider-short+}
2+
to connect to a **MongoDB Atlas cluster**. If you prefer to connect to MongoDB
3+
using another programming language, see our
4+
:driver:`list of official MongoDB drivers <>`.
5+
6+
The {+provider-short+} simplifies operations on data in MongoDB clusters by mapping the
7+
data to .NET objects.
8+
9+
MongoDB Atlas is a fully-managed cloud database service that hosts your data
10+
on MongoDB clusters. In this guide, we show you how to get started with your
11+
own free (no credit card required) cluster.
12+
13+
Follow the steps below to connect your {+provider-short+} application to a MongoDB Atlas
14+
cluster.

source/index.txt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1-
=======
2-
Snooty
3-
=======
1+
=================================
2+
MongoDB Entity Framework Provider
3+
=================================
44

5-
Our words here. Don't forget to add a toctree!
5+
.. toctree::
66

7-
Have a lovely day!
7+
/quick-start
8+
/quick-reference
89

10+
Introduction
11+
------------
12+
13+
Welcome to the documentation site for the official {+provider-long+}.
14+
You can add the provider to your .NET application as an object-relational mapper (O/RM)
15+
to work with data in MongoDB.
16+
Download the provider by using `NuGet <https://www.nuget.org/packages/{+package+}>`__,
17+
or set up a runnable project by following our :ref:`Quick Start guide <entity-framework-quickstart>`.
18+
19+
Quick Start
20+
-----------
21+
22+
Learn how to establish a connection to MongoDB Atlas and begin
23+
working with data in the :ref:`entity-framework-quickstart` section.
24+
25+
Quick Reference
26+
---------------
27+
28+
See driver syntax examples for common MongoDB commands in the
29+
:ref:`Quick Reference <entity-framework-quick-reference>` section.

source/quick-reference.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.. _entity-framework-quick-reference:

source/quick-start.txt

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
.. _entity-framework-quickstart:
2+
3+
===========
4+
Quick Start
5+
===========
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. include:: /includes/quick-start/overview.rst
14+
15+
Create a MongoDB Cluster
16+
------------------------
17+
18+
.. procedure::
19+
:style: connected
20+
21+
.. step:: Set Up a Free Tier Cluster in Atlas
22+
23+
To set up your Atlas free cluster required for this Quick Start, complete the guide
24+
:guides:`MongoDB Atlas Setup </atlas/account>` guide.
25+
26+
After completing the steps in the Atlas guide, you have a new MongoDB
27+
cluster deployed in Atlas, a new database user, and
28+
sample datasets loaded into your cluster. You also have
29+
a connection string similar to the following in your copy buffer:
30+
31+
.. code-block:: bash
32+
:copyable: false
33+
34+
"mongodb+srv://<username>:<password>@cluster0.abc.mongodb.net/?retryWrites=true&w=majority"
35+
36+
.. step:: Update the Placeholders
37+
38+
Paste the connection string in your copy buffer into a file in your preferred text
39+
editor. Replace the ``<username>`` and ``<password>`` placeholders with
40+
your database user's username and password.
41+
42+
Save this file to a safe location for use in the next step.
43+
44+
.. step:: Add Your Connection String to an Environment Variable
45+
46+
Run the following code in your shell to save the MongoDB connection string
47+
in your copy buffer from the previous step to an
48+
environment variable. Storing your connection string in an
49+
environment variable keeps your credentials separate from your source code. This
50+
separation makes it less likely to expose your credentials when sharing your code.
51+
52+
.. code-block:: bash
53+
54+
export MONGODB_URI='<your connection string>'
55+
56+
.. important::
57+
58+
Make sure to replace the ``<username>`` and ``<password>`` sections of the connection
59+
string with the username and password of your database user.
60+
61+
Set Up Your Project
62+
-------------------
63+
64+
.. procedure::
65+
:style: connected
66+
67+
.. step:: Create the Project
68+
69+
Create a new directory and use the ``dotnet new`` command to initialize your project
70+
as follows:
71+
72+
.. code-block:: shell
73+
74+
mkdir entity-quickstart
75+
cd entity-quickstart
76+
dotnet new console
77+
78+
.. _entity-framework-add-mongodb-dependency:
79+
80+
.. step:: Add the {+provider-long+} as a Dependency
81+
82+
Use the ``dotnet add`` command to add the {+provider-short+} to your project as a
83+
dependency.
84+
85+
.. code-block:: shell
86+
87+
dotnet add package {+package+}
88+
89+
Query Your MongoDB Cluster from Your Application
90+
------------------------------------------------
91+
92+
.. procedure::
93+
:style: connected
94+
95+
.. step:: Add the Sample Code
96+
97+
Open the file named ``Program.cs`` in the base directory of your project. Copy the
98+
following sample code into ``Program.cs``:
99+
100+
.. literalinclude:: /includes/quick-start/Program.cs
101+
:language: csharp
102+
:dedent:
103+
104+
.. step:: Query the Sample Data
105+
106+
Run the following command in your shell. It should print the plot of the movie
107+
"Back to the Future" from the sample dataset:
108+
109+
.. io-code-block::
110+
:copyable: true
111+
112+
.. input::
113+
:language: none
114+
115+
dotnet run entity-quickstart.csproj
116+
117+
.. output::
118+
:language: none
119+
:visible: false
120+
121+
A young man is accidentally sent 30 years into the past in a time-traveling
122+
DeLorean invented by his friend, Dr. Emmett Brown, and must make sure his
123+
high-school-age parents unite in order to save his own existence.
124+
125+
.. tip::
126+
127+
If your output is empty, ensure you have loaded the
128+
:atlas:`sample datasets </sample-data/>` into your cluster.
129+
130+
After completing these steps, you should have a working {+framework+} application that
131+
connects to your MongoDB cluster, runs a query on the
132+
sample data, and prints out the result.
133+
134+
Next Steps
135+
----------
136+
137+
Learn how to use the {+provider-short+} to perform common operations in Quick Reference.

0 commit comments

Comments
 (0)