Use Spring Data with Azure Cosmos DB for MongoDB API

This article demonstrates creating a sample application that uses Spring Data to store and retrieve information using Azure Cosmos DB for MongoDB.

Prerequisites

  • A Git client.

Create an Azure Cosmos DB account

Create an Azure Cosmos DB account using the Azure portal

Note

You can read more detailed information about creating accounts in the Azure Cosmos DB documentation.

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Select Create a resource, then Databases, then Azure Cosmos DB.

  3. On the Select API option screen, select Azure Cosmos DB for MongoDB.

    Azure portal, create a resource, select API option, Azure Cosmos DB for MongoDB selected.

  4. Specify the following information:

    • Subscription: Specify your Azure subscription to use.
    • Resource group: Specify whether to create a new resource group, or choose an existing resource group.
    • Account name: Choose a unique name for your Azure Cosmos DB account; this will be used to create a fully-qualified domain name like wingtiptoysmongodb.documents.azure.com.
    • API: Specify Azure Cosmos DB for MongoDB API for this tutorial.
    • Location: Specify the closest geographic region for your database.
  5. When you've entered all of the above information, click Review + create.

  6. If everything looks correct on the review page, click Create.

    Review your Azure Cosmos DB account settings.

Retrieve the connection string for your Azure Cosmos DB account

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Click All Resources, then click the Azure Cosmos DB account you just created.

  3. Click Connection strings, and copy the value for the Primary Connection String field; you'll use that value to configure your application later.

    Retrieve your Azure Cosmos DB connection string.

Configure the sample application

  1. Open a command shell and clone the sample project using a git command like the following example:

    git clone https://github.com/spring-guides/gs-accessing-data-mongodb.git
    
  2. Create a resources directory in the <project root>/complete/src/main directory of the sample project, and create an application.properties file in the resources directory.

  3. Open the application.properties file in a text editor, and add the following lines in the file, and replace the sample values with the appropriate values from earlier:

    spring.data.mongodb.database=wingtiptoysmongodb
    spring.data.mongodb.uri=mongodb://wingtiptoysmongodb:AbCdEfGhIjKlMnOpQrStUvWxYz==@wingtiptoysmongodb.documents.azure.com:10255/?ssl=true&replicaSet=globaldb
    

    Where:

    Parameter Description
    spring.data.mongodb.database Specifies the name of your Azure Cosmos DB account from earlier in this article.
    spring.data.mongodb.uri Specifies the Primary Connection String from earlier in this article.
  4. Save and close the application.properties file.

Package and test the sample application

To build the application, browse to the directory /gs-accessing-data-mongodb/complete, which contains the pom.xml file.

  1. Build the sample application with Maven, and configure Maven to skip tests; for example:

    mvn clean package -DskipTests
    
  2. Start the sample application; for example:

    
    java -jar target/accessing-data-mongodb-complete-0.0.1-SNAPSHOT.jar
    

    Your application should return values like the following:

    Customers found with findAll():
    -------------------------------
    Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith']
    Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith']
    
    Customer found with findByFirstName('Alice'):
    --------------------------------
    Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith']
    Customers found with findByLastName('Smith'):
    --------------------------------
    Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith']
    Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith']
    

Summary

In this tutorial, you created a sample Java application that uses Spring Data to store and retrieve information using Azure Cosmos DB for MongoDB.

Clean up resources

When no longer needed, use the Azure portal to delete the resources created in this article to avoid unexpected charges.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

See also

For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.