Azure Cosmos DB Commands

◷ Reading Time: 1 minute

Create Document

Creates a document in your collection.

Summary
  • Data Source (required): connection string to your collection.
    E.g.endpoint=YourEndpoint;masterKey=YourMasterKey;database=YourDatabaseName;collection=YourCollectionName
  • Document Content (required): your json document’s content.
  • Partition Key: The partition key value if required by your collection.
    • Dynamically specify the key. E.g. @.LastName.
    • Explicitly specify the key. E.g. "Andersen".
    • Alternatively, you can pass the partition key in your Data Source.

Example

Create Document

1. Add a Create Document activity to your flow.

2. Set the arguments.

Enter the Data Source. The example below uses a database called SchoolDatabase, and a container called StaffContainer.

Enter the Document Content as a Json object.

Enter the Partition Key. This example uses a dynamic partition key which will grab the partition key value from the Document Content.

3. Execute your flow and browse to your Cosmos DB container to see your document created in the container.

Query Database

Runs a SQL query against the documents in your collection.

Summary
  • Data Source (required): connection string to your collection.
    E.g.endpoint=YourEndpoint;masterKey=YourMasterKey;database=YourDatabaseName;collection=YourCollectionName
  • SQL Query (required): CosmosDB compatible SQL query to run.
  • Partition Key: The partition key to use for your document.
    • Dynamically specify the key. E.g. @.LastName.
    • Explicitly specify the key. E.g. "Andersen".
    • Alternatively, you can pass the partition key in your Data Source.
  • returns: JSON of query results.

Example

Query Database

In this example we’re using a collection with two documents inside. We will execute a query against this collection to read the two documents.

1. Create a variable to store your results.

Right click on the canvas and click on Variable Parameters.

Create a local variable called results with the json Type and click OK.

2. Add a Query Database activity to your flow.

3. Set the arguments.

Enter the Data Source. The example below uses a database called SchoolDatabase, and a container called StaffContainer.

Enter the SQL Query you want to execute. In this example we’ll select all the documents in the container.

Enter the Partition Key. If you do not enter a Partition Key, a cross-partition query will be executed. In this example, we will not enter a Partition Key.

Enter the variable created in step 1 to your Copy Result To.

4. Run your flow in debug mode. Once you pass the Query Database activity, you should be able to see two documents loaded in to the results variable.

Update Document

Updates a document in your collection.

Summary
  • Data Source (required): connection string to your collection.
    E.g.endpoint=YourEndpoint;masterKey=YourMasterKey;database=YourDatabaseName;collection=YourCollectionName
  • Document Content (required): your JSON document’s content.
  • Document ID (required): The document ID for your document in the collection.
  • Partition Key: The partition key to use for your document.
    • Dynamically specify the key. E.g. @.LastName.
    • Explicitly specify the key. E.g. "Andersen".
    • Alternatively, you can pass the partition key in your Data Source.

Example

Update Document

Our Cosmos DB container has a document that we created in an earlier example. We will update the name for the document in this example.

1. Add a Update Document activity to the flow.

2. Set the arguments.

Enter the Data Source. The example below uses a database called SchoolDatabase, and a container called StaffContainer.

Enter the Document Content as a Json object.

Enter the Document ID.

Enter the Partition Key. This example uses a static value which needs to be wrapped in quotation marks.

3. Execute your flow and browse to your Cosmos DB container to see the change made to your document which should have the name changed.

Delete Document

Deletes a document in your collection.

Summary
  • Data Source (required): connection string to your collection.
    E.g.endpoint=YourEndpoint;masterKey=YourMasterKey;database=YourDatabaseName;collection=YourCollectionName
  • Document ID (required): The document ID for your document in the collection.
  • Partition Key: The partition key to use for your document.
    • Dynamically specify the key. E.g. @.LastName.
    • Explicitly specify the key. E.g. "Andersen".
    • Alternatively, you can pass the partition key in your Data Source.

Example

Delete Document

Our Cosmos DB container has a document that we created in an earlier example. We will delete this document in this example.

1. Add a Delete Document activity to your flow.

2. Set the arguments.

Enter the Data Source. The example below uses a database called SchoolDatabase, and a container called StaffContainer.

Enter the Document ID.

Enter the Partition Key. This example uses a static value.

3. Execute your flow and browse to your Cosmos DB container to see that the document has been deleted.

Updated on February 26, 2020

Was this article helpful?

Related Articles