Quickstart: Use Azure Cosmos DB for MongoDB (RU) with .NET

In this quickstart, you deploy a basic Azure Cosmos DB for MongoDB application using Python. Azure Cosmos DB for MongoDB is a schemaless data store allowing applications to store unstructured documents in the cloud with MongoDB libraries. You learn how to create documents and perform basic tasks within your Azure Cosmos DB resource using Python.

Library source code | Package (NuGet) | Azure Developer CLI

Prerequisites

  • Azure Developer CLI
  • Docker Desktop
  • .NET SDK 9.0

If you don't have an Azure account, create a free account before you begin.

Initialize the project

Use the Azure Developer CLI (azd) to create an Azure Cosmos DB for Table account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.

  1. Open a terminal in an empty directory.

  2. If you're not already authenticated, authenticate to the Azure Developer CLI using azd auth login. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.

    azd auth login
    
  3. Use azd init to initialize the project.

    azd init --template cosmos-db-mongodb-dotnet-quickstart
    
  4. During initialization, configure a unique environment name.

  5. Deploy the Azure Cosmos DB account using azd up. The Bicep templates also deploy a sample web application.

    azd up
    
  6. During the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can take approximately five minutes.

  7. Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.

    Deploying services (azd deploy)
    
      (✓) Done: Deploying service web
    - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>
    
    SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
    
  8. Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.

Screenshot of the running web application.

Install the client library

The client library is available through NuGet, as the MongoDB.Driver package.

  1. Open a terminal and navigate to the /src/web folder.

    cd ./src/web
    
  2. If not already installed, install the MongoDB.Driver package using dotnet add package.

    dotnet add package MongoDB.Driver
    
  3. Open and review the src/web/Microsoft.Samples.Cosmos.MongoDB.Quickstart.Web.csproj file to validate that the MongoDB.Driver entry exists.

Object model

Name Description
MongoClient Type used to connect to MongoDB.
Database Represents a database in the account.
Collection Represents a collection within a database in the account.

Code examples

The sample code in the template uses a database named cosmicworks and collection named products. The products collection contains details such as name, category, quantity, and a unique identifier for each product. The collection uses the /category property as a shard key.

Authenticate the client

This sample creates a new instance of the MongoClient class.

string connectionString = "<azure-cosmos-db-for-mongodb-connection-string>";

MongoClient client = new(connectionString);

Get a database

This sample creates an instance of the IMongoDatabase interface using the GetDatabase method of the MongoClient class.

IMongoDatabase database = client.GetDatabase("<database-name>");

Get a collection

This sample creates an instance of the generic IMongoCollection<> interface using the GetCollection<> generic method of the IMongoDatabase interface. The generic interface and method both uses a type named Product defined in another class.

IMongoCollection<Product> collection = database.GetCollection<Product>("<collection-name>");
public record Product(
    string id,
    string category,
    string name,
    int quantity,
    decimal price,
    bool clearance
);

Create a document

Create a document in the collection using collection.ReplaceOneAsync<> with the generic Product type parameter. This method "upserts" the item effectively replacing the item if it already exists.

Product document = new(
    id: "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    category: "gear-surf-surfboards",
    name: "Yamba Surfboard",
    quantity: 12,
    price: 850.00m,
    clearance: false
);

await collection.ReplaceOneAsync<Product>(
    d => d.id == document.id,
    document,
    new ReplaceOptions { IsUpsert = true }
);

Read a document

Perform a point read operation by using both the unique identifier (id) and shard key fields. Use collection.FindAsync<> with the generic Product type parameter to efficiently retrieve the specific item.

IAsyncCursor<Product> documents = await collection.FindAsync<Product>(
    d => d.id == "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" && d.category == "gear-surf-surfboards"
);

Product? document = await documents.SingleOrDefaultAsync();

Query documents

Perform a query over multiple items in a container using collection.AsQueryable() and language-integrated query (LINQ). This query finds all items within a specified category (shard key).

IQueryable<Product> documents = collection.AsQueryable().Where(
    d => d.category == "gear-surf-surfboards"
);

foreach (Product document in await documents.ToListAsync())
{
    // Do something with each item
}

Explore your data

Use the Visual Studio Code extension for Azure Cosmos DB to explore your MongoDB data. You can perform core database operations including, but not limited to:

  • Performing queries using a scrapbook or the query editor
  • Modifying, updating, creating, and deleting documents
  • Importing bulk data from other sources
  • Managing databases and collections

For more information, see How-to use Visual Studio Code extension to explore Azure Cosmos DB for MongoDB data.

Clean up resources

When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

azd down