Skip to content

Latest commit

 

History

History
132 lines (82 loc) · 6.02 KB

how-to-dotnet-get-started.md

File metadata and controls

132 lines (82 loc) · 6.02 KB
title description author ms.author ms.service ms.subservice ms.devlang ms.topic ms.date ms.custom
Get started with Azure Cosmos DB for MongoDB using .NET
Get started developing a .NET application that works with Azure Cosmos DB for MongoDB. This article helps you learn how to set up a project and configure access to an Azure Cosmos DB for MongoDB database.
gahl-levy
gahllevy
azure-cosmos-db
mongodb
csharp
how-to
10/17/2022
devx-track-dotnet, devguide-csharp, cosmos-db-dev-journey

Get started with Azure Cosmos DB for MongoDB using .NET

[!INCLUDEMongoDB]

This article shows you how to connect to Azure Cosmos DB for MongoDB using .NET Core and the relevant NuGet packages. Once connected, you can perform operations on databases, collections, and documents.

Note

The example code snippets are available on GitHub as a .NET Core project.

API for MongoDB reference documentation | MongoDB Package (NuGet)

Prerequisites

Create a new .NET Core app

  1. Create a new .NET Core application in an empty folder using your preferred terminal. For this scenario, you'll use a console application. Use the dotnet new command to create and name the console app.

    dotnet new console -o app
  2. Add the MongoDB NuGet package to the console project. Use the dotnet add package command specifying the name of the NuGet package.

    dotnet add package MongoDB.Driver
  3. To run the app, use a terminal to navigate to the application directory and run the application.

    dotnet run

Connect to Azure Cosmos DB for MongoDB with the MongoDB native driver

To connect to Azure Cosmos DB with the MongoDB native driver, create an instance of the MongoClient class. This class is the starting point to perform all operations against MongoDb databases. The most common constructor for MongoClient accepts a connection string, which you can retrieve using the following steps:

Get resource name

[!INCLUDE Azure CLI - get resource name]

[!INCLUDE Powershell - set resource name]

Skip this step and use the information for the portal in the next step.


Retrieve your connection string

[!INCLUDE Azure CLI - get connection string]

[!INCLUDE Powershell - get connection string]

Tip

For this guide, we recommend using the resource group name msdocs-cosmos.

[!INCLUDE Portal - get connection string]


Configure environment variables

[!INCLUDE Multitab - store connection string in environment variable]

Create MongoClient with connection string

Define a new instance of the MongoClient class using the constructor and the connection string variable you set previously.

:::code language="csharp" source="~/azure-cosmos-mongodb-dotnet/101-manage-connection/program.cs" id="client_credentials":::

Use the MongoDB client classes with Azure Cosmos DB for API for MongoDB

[!INCLUDE Conceptual object model]

Each type of resource is represented by one or more associated C# classes. Here's a list of the most common classes:

Class Description
MongoClient This class provides a client-side logical representation for the API for MongoDB layer on Azure Cosmos DB. The client object is used to configure and execute requests against the service.
MongoDatabase This class is a reference to a database that may, or may not, exist in the service yet. The database is validated or created server-side when you attempt to perform an operation against it.
Collection This class is a reference to a collection that also may not exist in the service yet. The collection is validated server-side when you attempt to work with it.

The following guides show you how to use each of these classes to build your application and manage data.

Guide:

See also

Next steps

Now that you've connected to an API for MongoDB account, use the next guide to create and manage databases.

[!div class="nextstepaction"] Create a database in Azure Cosmos DB for MongoDB using .NET