Azure Cosmos DB for MongoDB Tips

Sanity check cost of most used query

Cosmos DB charges based on “request unit” used. So it is useful to check the request unit cost of the most used query. A high cost also often indicates bad performing query, so it really is a lose-lose situation. You most likely want to redesign your query, or add a new index to your table. Here are the steps I took to find out the request unit cost using mongosh , more info can be found directly from Microsoft: Find request unit charge for Azure Cosmos DB API for MongoDB operations

  1. Install mongosh .
  2. mongosh "<connection_string>"
    Be sure to double quote the connection string.
  3. show dbs
  4. use <db_name>
  5. Run the query in concern:
    Sanity check that the output is what you expect.
    Add .toArray() at the end if your query
  6. Immediately following the query run: db.runCommand({getLastRequestStatistics: 1})
  7. Repeat the last two steps quickly to make sure you are really looking at the stats of the query you are testing, not another query that came in between the two commands.
  8. RequestCharge is where you’ll find the cost. Typically an efficient query should have the value under 10, and often under 5:
CommandName: 'find',
RequestCharge: 7.3,
RequestDurationInMilliSeconds: Long("46"),
EstimatedDelayFromRateLimitingInMilliseconds: Long("0"),
RetriedDueToRateLimiting: false,
ActivityId: '9f6f8c8a-8ed0-45f3-a4e6-0c39fc3f1013',
ok: 1



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store