Creating DocumentDB and Performing CRUD operations on DocumentDB via Logic Apps
Introduction
DocumentDB, as a NoSQL store, is truly schema-free. It allows you to store and query any JSON document, regardless of schema. The service provides built-in automatic indexing support – which means you can write JSON documents to the store and immediately query them using a familiar document oriented SQL query grammar. DocumentDB is designed to linearly scale to meet the needs of an application.
In this article, we will learn how to perform CRUD operations on DocumentDB via Logic Apps.
Create Account in DocumentDB
For the creation of DocumentDB, we first need to create a DocumentDB account.
- ID: Name of the DocumentDB account.
- Resource Group: Either create a new resource group or use existing.
- Location: Choose the Microsoft data center you want this account to be hosted.
Once you click on create, the DocumentDB account will be created.
Create Database in DocumentDB
Next step is to add a database in DocumentDB, for which you will have to open the DocumentDB account you have created. Go to Browse and Add Database.
- ID: Name of the DocumentDB database
Create Collection in DocumentDB
Next step is to add a collection to the database of DocumentDB, for which you will have to open the DocumentDB account you have created. Go to Browse and Add Collection.
- Collection Id: Name of the DocumentDB collection.
- Storage Capacity: By default, Storage Capacity is set to 250 GB to handle partitioned collections.
- Partition Key: Enter a partition key for the collection. This is required for partitioned collections and optional for single partition collections.
- Database: Either create a new database or use an existing one.
Create and Update Documents in DocumentDB via Logic Apps
Create a new logic app. This logic app will be used to Insert and Update documents in DocumentDB.
Trigger
Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app.
- HTTP POST to this URL: This URL will be created automatically once you save the logic app and it will be used as an endpoint by the REST client to invoke this logic app.
- Request Body JSON Schema: This is an optional property, which validates the incoming request. Useful for helping subsequent workflow steps know which properties to reference.
Actions
1) Create and Update Document
This action can be used under DocumentDB activity for inserting new documents and updating the existing document.
- Database ID: Specify the database where the document should be created or updated.
- Collection ID: Specify the collection where the document should be created or updated.
- Document: The document, which needs to be inserted, in our case it will be the body of the request made by the REST client.
- IsUpsert: If set to true, the document will be replaced if it exists else created.
2) Send an email
This action can be used under Outlook 365 connector, to trigger an email.
- Body: Body of the message.
- Subject: Subject of the message.
- To: The email addresses of the recipients.
We used Boomerang as the REST client to trigger the logic apps available as an extension in Google Chrome: https://chrome.google.com/webstore/detail/boomerang-soap-rest-clien/eipdnjedkpcnlmmdfdkgfpljanehloah?hl=en
The logic app's run was successful and the document was inserted into the collection specified.
We received an email to the specified email address as well with the message body.
Upon checking, the collection we could see that the document has be successfully created in the collection specified.
Similarly, the same document was successfully updated in DocumentdB in the next hit.
Upon checking the collection we could see that the document has be successfully updated in the collection specified.
Read Documents from DocumentDB via Logic Apps
Create a new logic app. This logic app will be used to read documents from DocumentDB.
Trigger
Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app.
1)** HTTP POST to this URL**: This URL will be created automatically once you save the logic app and it will be used as an endpoint by the REST client to invoke this logic app.
2) Request Body JSON Schema: This is an optional property, which validates the incoming request. Useful for helping subsequent workflow steps know which properties to reference.
Actions
1) Get Document
This action can be used under DocumentDB connector for fetching the documents from the DocumentDB.
- Database ID: Specify the database where the document should be created or updated.
- Collection ID: Specify the collection where the document should be created or updated.
- Document ID: The ID of the document, which needs to fetched from the DocumentDB.
2)
Response
This action type contains the entire response payload from an HTTP request and includes a statusCode, body, and headers:
- Status Code: The HTTP status code
- Headers: A JSON object of any response headers to include
- Body: The response body
We used Boomerang as the REST client to trigger the logic apps available as an extension in Google Chrome: https://chrome.google.com/webstore/detail/boomerang-soap-rest-clien/eipdnjedkpcnlmmdfdkgfpljanehloah?hl=en
The logic apps run was successful and it was able to fetch the document with the document id ‘abc3’ from DocumentDB.
The response was displayed back on Boomerang.
Delete Documents from DocumentDB via Logic Apps
Create a new logic app. This logic app will be used to delete documents from DocumentDB.
Trigger
Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app.
1) HTTP POST to this URL: This URL will be created automatically once you save the logic app and it will be used as an endpoint by the REST client to invoke this logic app.
2) Request Body JSON Schema: This is an optional property, which validates the incoming request. Useful for helping subsequent workflow steps know which properties to reference.
Actions
1) Delete a Document
This action can be used under DocumentDB connector for deleting the documents from the DocumentDB.
- Database ID: Specify the database where the document should be created or updated.
- Collection ID: Specify the collection where the document should be created or updated.
- Document ID: The ID of the document, which needs to deleted from the DocumentDB.
3)
Response
This action type contains the entire response payload from an HTTP request and includes a statusCode, body, and headers:
- Status Code: The HTTP status code
- Headers: A JSON object of any response headers to include
- Body: The response body
We used Boomerang as the REST client to trigger the logic apps available as an extension in Google Chrome: https://chrome.google.com/webstore/detail/boomerang-soap-rest-clien/eipdnjedkpcnlmmdfdkgfpljanehloah?hl=en
The logic apps run was successful and it was able to delete the document with the document id ‘abc2’ from the documentDb.
The response was displayed back on Boomerang.
Moreover, the document was not visible anymore in the collection.