Find the request unit charge for operations executed in Azure Cosmos DB for MongoDB

APPLIES TO: MongoDB

Azure Cosmos DB supports many APIs, such as SQL, MongoDB, Cassandra, Gremlin, and Table. Each API has its own set of database operations. These operations range from simple point reads and writes to complex queries. Each database operation consumes system resources based on the complexity of the operation.

The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). Request charge is the request units consumed by all your database operations. You can think of RUs as a performance currency abstracting the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB. No matter which API you use to interact with your Azure Cosmos DB container, costs are always measured by RUs. Whether the database operation is a write, point read, or query, costs are always measured in RUs. To learn more, see the request units and it's considerations article.

This article presents the different ways you can find the request unit (RU) consumption for any operation executed against a container in Azure Cosmos DB for MongoDB. If you're using a different API, see API for NoSQL, API for Cassandra, API for Gremlin, and API for Table articles to find the RU/s charge.

The RU charge is exposed by a custom database command named getLastRequestStatistics. The command returns a document that contains the name of the last operation executed, its request charge, and its duration. If you use the Azure Cosmos DB for MongoDB, you have multiple options for retrieving the RU charge.

Use the Azure portal

  1. Sign in to the Azure portal.

  2. Create a new Azure Cosmos DB account and feed it with data, or select an existing account that already contains data.

  3. Go to the Data Explorer pane, and then select the container you want to work on.

  4. Select the ... next to the container name and select New Query.

  5. Enter a valid query, and then select Execute Query.

  6. Select Query Stats to display the actual request charge for the request you executed. This query editor allows you to run and view request unit charges for only query predicates. You can't use this editor for data manipulation commands such as insert statements.

    Screenshot of a MongoDB query request charge in the Azure portal

  7. To get request charges for data manipulation commands, run the getLastRequestStatistics command from a shell based UI such as Mongo shell, Robo 3T, MongoDB Compass, or a VS Code extension with shell scripting.

    db.runCommand({getLastRequestStatistics: 1})

Programmatically

When you use the Mongo shell, you can execute commands by using runCommand().

db.runCommand('getLastRequestStatistics')

Next steps

To learn about optimizing your RU consumption, see these articles: