|
| 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