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

APPLIES TO: Azure Cosmos DB API for 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 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 API for MongoDB. If you're using a different API, see SQL API, Cassandra API, Gremlin API, and Table API 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 API 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 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})

Use a MongoDB driver

When you use the official MongoDB .NET driver, you can execute commands by calling the RunCommand method on a IMongoDatabase object. This method requires an implementation of the Command<> abstract class:

class GetLastRequestStatisticsCommand : Command<Dictionary<string, object>>
    public override RenderedCommand<Dictionary<string, object>> Render(IBsonSerializerRegistry serializerRegistry)
        return new RenderedCommand<Dictionary<string, object>>(new BsonDocument("getLastRequestStatistics", 1), serializerRegistry.GetSerializer<Dictionary<string, object>>());

Dictionary<string, object> stats = database.RunCommand(new GetLastRequestStatisticsCommand());
double requestCharge = (double)stats["RequestCharge"];

For more information, see Quickstart: Build a .NET web app by using an Azure Cosmos DB API for MongoDB.

Next steps

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