How to use Spring Data MongoDB API with Azure Cosmos DB

Overview

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

Prerequisites

The following prerequisites are required in order to complete the steps in this article:

Create an Azure Cosmos DB account

Create a Cosmos DB account using the Azure portal

Note

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

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

  2. Click +Create a resource, then Databases, and then click Azure Cosmos DB.

    Create an Azure Cosmos DB account

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

    Specify your Cosmos DB account settings

  4. When you have entered all of the above information, click Review + create.

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

    Review your 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 will use that value to configure your application later.

    Retrieve your 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 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-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 the Azure Cosmos DB MongoDB API.

Next steps

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

Additional Resources

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