Azure Cosmos DB Shell quick start

In this guide, you connect to an account, verify its identity, inspect a container, and run a bounded query. Optional steps create a sample database and demonstrate item operations and file exchange.

Prerequisites

  • Install Azure Cosmos DB Shell.
  • An Azure Cosmos DB account and permission to access its data. Use a nonproduction account for the optional write examples.
  • Ensure that your machine can reach the account through its configured firewall and private-network rules.
  • For Microsoft Entra authentication, the intended tenant ID and appropriate Azure Cosmos DB data-plane permissions. Resource-management actions can require separate management-plane permissions.

Connect and verify the account

Start the shell from your operating-system terminal:

cosmosdbshell

Run the following commands inside the shell. Replace the account name and tenant ID, and then complete the browser sign-in:

connect https://<account-name>.documents.azure.com/ --tenant=<tenant-id>
info --format=json

At this account-root scope, review accountId and uri and confirm that they identify your intended account. Stop if they don't match. pwd shows the database/container navigation path, not account identity.

For an existing default credential chain, such as a configured development identity or system-assigned managed identity, omit --tenant:

connect https://<account-name>.documents.azure.com/

For a user-assigned managed identity in a supported Azure environment, use --managed-identity=<client-id>. A connection doesn't grant permissions automatically.

Prefer Microsoft Entra ID to account keys. If you use a connection string, quote the entire string and keep it out of source control and chat. Shell history can retain connection commands.

Discover databases and containers

List databases, and then substitute one of their actual names for MyDatabase:

ls
ls --database=MyDatabase

Select an existing container from that database, substitute its name for MyContainer, and inspect a bounded sample:

schema --database=MyDatabase --container=MyContainer --sample=20

Review the partition key paths, field names, and observed types. Schema inference describes sampled documents, not every document in the container.

You can also navigate interactively:

cd /MyDatabase/MyContainer
pwd

The remaining read examples explicitly name their targets. This approach avoids depending on navigation that another MCP client or command might change.

Run a bounded query

Return up to five IDs:

query "SELECT TOP 5 c.id FROM c" --database=MyDatabase --container=MyContainer --max=5

Interactive query has no default result limit, so supply a positive --max. Queries consume RUs even when they return a small result. Project only the fields you need.

If you know an item's exact ID and complete partition key, use a print point read rather than a query. Preserve the partition key value's type; for example, numeric 680 and string '"680"' target different partitions.

Diagnose access

Run read-only diagnostics before changing account settings or credentials:

doctor --no-update-check --format=json
whoami
can-i query --database=MyDatabase --container=MyContainer

doctor distinguishes passed, warning, failed, and skipped checks. Its default update check contacts public GitHub releases; --no-update-check disables that lookup. can-i query performs a small query probe and consumes RUs. A probe isn't a complete audit of assigned permissions.

Try sample writes

Use this optional section only in a nonproduction account where you intend to create data. The sample uses a new database named ShellDemo, a container named Items, and /tenantId as its partition key.

Important

The following commands create resources and write data. On provisioned-throughput accounts, the example configures database autoscale and dedicated container throughput, both of which can incur charges. Use unused resource names and verify the connected account before continuing.

Create sample resources

Return to the account root, verify the account, and list databases before choosing an unused database name:

cd
info --format=json
ls

mkdb requires account-root scope. Run each creation command separately and check its result before continuing. If creation reports an existing resource, stop and choose unused names rather than reusing it.

On a provisioned-throughput account:

mkdb ShellDemo --scale=auto --ru=1000
cd /ShellDemo
mkcon Items /tenantId --scale=manual --ru=400

On a serverless account, omit throughput options instead:

mkdb ShellDemo
cd /ShellDemo
mkcon Items /tenantId

Run only the sequence appropriate for your account. If either resource already exists, stop and choose unused names rather than modifying unrelated data.

Create and read an item

Create one sample item, and then verify it with a point read:

mkitem '{"id":"sample-1","tenantId":"demo","status":"new","quantity":1}' --database=ShellDemo --container=Items
print sample-1 demo --database=ShellDemo --container=Items

The string demo is the item's /tenantId partition key value.

Patch a field

Change only the sample item's status, and then verify the result:

patch set sample-1 demo /status reviewed --database=ShellDemo --container=Items
print sample-1 demo --database=ShellDemo --container=Items

For concurrent workflows, read the current item and supply its actual ETag with --etag to avoid changing an unreviewed version.

Export and validate an import

Export just the sample item to a new local file:

export sample-items.json --database=ShellDemo --container=Items --query="SELECT * FROM c WHERE c.tenantId = 'demo' AND c.id = 'sample-1'" --max=1
import sample-items.json --database=ShellDemo --container=Items --dry-run

The exported file is a JSON array that import can read directly. No jq conversion is needed. A .csv destination selects CSV, but CSV imports convert values to strings and aren't a typed JSON roundtrip.

The import dry run is offline: it validates syntax and item IDs without contacting the target. It reports validated records and imports zero items. It doesn't establish target permissions, partition key compatibility, or whether IDs already exist.

To copy the file to another compatible container, supply that destination and remove --dry-run only after review. The default insert mode fails on existing IDs. --mode=upsert can overwrite them. Don't import this example back into the original container unless that behavior is intended.

For transactional batches and independent cross-partition writes, see Transactions and bulk operations.

Clean up sample resources

Warning

Deleting the sample database removes every container and item in it. Continue only if you created it for this guide and don't need its contents.

Return to the account root, verify that accountId and uri identify the intended account, and preview the database deletion:

cd
info --format=json
rmdb ShellDemo --dry-run

Verify the account and database again. If the preview identifies only the database created for this guide, run rmdb ShellDemo and approve its interactive confirmation. Use ls at the account root afterward to verify removal. Don't delete a pre-existing database to clean up these examples.

If you want to delete only the sample item instead, follow the exact-item deletion procedure, including the actual preview ETag.

Remove sample-items.json with your operating system's file tools when you no longer need the local copy. Exit the shell with:

exit

AI tools

Use these actions to discuss your first Shell session with your assistant.

For documentation-only retrieval, use the Microsoft Learn MCP server. For a compact guide index, use the Shell documentation index (llms.txt). The index doesn't change crawler permissions or guarantee retrieval.

Shell MCP requires separate setup and can read or change data under the configured permissions, so review tool calls before approving them. Don't share credentials or sensitive data.

Connect your AI agent
Configure Shell MCP for authenticated database operations.

Read as Markdown
Open a text version to copy into your assistant.

Ask ChatGPT
Plan a first session and understand each command.

Ask Claude
Plan a first session and understand each command.

ChatGPT and Claude receive only the public article URL and a starter prompt. You might need to sign in. If retrieval fails, copy the Markdown text. These links don't configure database access.