Share via


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.

https://1.bp.blogspot.com/-KstxQCGOa9c/WP84DwrPYFI/AAAAAAAAB-A/oJjI7MUz2iQwEOAm-9e0saB8XvMl_VzzQCLcB/s640/1.png

  • 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.

https://4.bp.blogspot.com/-F4o_G0Ha-xU/WQDSWtTKooI/AAAAAAAAB-U/u7ZjBKp4668qdOyuT0vYg9Mh4Curl1IowCLcB/s640/2.png

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.

https://1.bp.blogspot.com/-rkTuEQW_kOM/WQDUuhhL4UI/AAAAAAAAB-g/xTD2t1TcSIIfEf687SZMOYmNBbZOlKfIQCLcB/s640/3.png

  • 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.

https://2.bp.blogspot.com/-1vHSdiTD3EI/WQEbJPLL_9I/AAAAAAAAB-w/JIhe06f-0HA7XdA27PhN8ospT3EMzOTIACLcB/s640/4.png

  • 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.

https://3.bp.blogspot.com/-WIAExS581JE/WQEbXzAdcZI/AAAAAAAAB-0/3rDBg9mEvH0cVZ0_8oJe0C6X1aVJqDRTQCLcB/s640/5.png

Trigger

Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app.
 

https://1.bp.blogspot.com/-gYnzHuQXj7c/WQEbkWiwrVI/AAAAAAAAB-4/LueOeBinM0Y-ZER9Rq4V9pgKlS3mAy7HQCLcB/s640/6.png

  • 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.

https://4.bp.blogspot.com/-RnXhLudI6Yo/WQEb1MrsCEI/AAAAAAAAB-8/RKGdLbdCZBo7dDp8UWmkNs3vKIx1BVnGQCLcB/s640/7.png

https://1.bp.blogspot.com/-R1dspMHX4xM/WQEb6YfK-mI/AAAAAAAAB_A/wEaV2iNlhD0fEsRpnyopI24Qvx8W-f1DACLcB/s640/8.png

  • 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.

https://3.bp.blogspot.com/-iQ04ctUNJHk/WQI3CnRoQqI/AAAAAAAACCs/-tPH3jrb3gksfj4boIF6syqfq-Z9-gS4QCLcB/s640/9.png

  • 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

https://3.bp.blogspot.com/-cVW2pSgeimU/WQEclWLUJ-I/AAAAAAAAB_M/Q9MFMryRInQLpaYApe-l-AwAWuS9_59MgCLcB/s640/10.png

The logic app's run was successful and the document was inserted into the collection specified. 

https://4.bp.blogspot.com/-MKWrDX8UaRQ/WQEcxYS0BwI/AAAAAAAAB_Q/IBJDNacYJ6YkPobU4XSTnPs7NYhSZIoAACLcB/s640/11.png

We received an email to the specified email address as well with the message body.

https://4.bp.blogspot.com/-qGKSnZqTZEk/WQEc5VJcLaI/AAAAAAAAB_U/MYrK2TzvXAUCM1HQGHL-H-UBMDUiwJ6sACLcB/s640/12.png

Upon checking, the collection we could see that the document has be successfully created in the collection specified.

https://1.bp.blogspot.com/-1WmfCsXvUYM/WQEdB-p8kSI/AAAAAAAAB_Y/UG5ZVs5Jr3cCjkjfBkAgpEZigKBcsDdxwCLcB/s640/13.png

Similarly, the same document was successfully updated in DocumentdB in the next hit.

https://2.bp.blogspot.com/-Zq-mTlV1H5c/WQEdSwpFoKI/AAAAAAAAB_c/u9Rk1gFiHrcmQPLq-valfMnp16nXNX_pQCLcB/s640/14.png

https://2.bp.blogspot.com/-nm05PZAyc5k/WQEdTNOnuZI/AAAAAAAAB_g/gKxfi1YGE1cc6BgfkOMoTGmZYPR7YdAuwCEw/s640/15.png

Upon checking the collection we could see that the document has be successfully updated in the collection specified.

https://1.bp.blogspot.com/-ySfoCkpjfIU/WQEdTHWum0I/AAAAAAAAB_k/ne4N8Er4XqsiIMWxyNugen4oW4F44jcZACEw/s640/16.png

Read Documents from DocumentDB via Logic Apps

Create a new logic app. This logic app will be used to read documents from DocumentDB.

https://1.bp.blogspot.com/-VLR7KD_Tg0M/WQEd9YJiFvI/AAAAAAAAB_w/ol0mBjDmaywt_2d6HysNq_hs7rDMinvNQCLcB/s640/17.png

Trigger

Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app. 

https://4.bp.blogspot.com/-EUk3Rvo_LCM/WQEd9PTGX-I/AAAAAAAAB_s/cMqf0YijpS4EAOg8GqnT6_NXXrFeYhHfQCEw/s640/18.png

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.

https://1.bp.blogspot.com/-VGP-4KWGlbM/WQEePYmp0jI/AAAAAAAAB_0/hSKYSI7igO4vRGwvgYIVPltwj6XhaiA1QCLcB/s640/19.png

  • 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:

https://3.bp.blogspot.com/-d2lzmCQp0nA/WQEeqd0MNwI/AAAAAAAAB_4/uzCi9jI25Z8PI3AtIsf0CJ8XRY_BUyc7wCLcB/s640/20.png

  • 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

https://4.bp.blogspot.com/-gcsOzAIO-ME/WQEe4xJInaI/AAAAAAAAB_8/a-gY3QlYnfs7tpd2U3sh4LHF_CAI6OUgQCLcB/s640/21.png

The logic apps run was successful and it was able to fetch the document with the document id ‘abc3’ from DocumentDB.

https://3.bp.blogspot.com/-Ca5fsU6z_wc/WQEfC-FED-I/AAAAAAAACAA/DEPLDnB0A7Qs_BHXZCBTD4h36hoGEqscgCLcB/s640/22.png

The response was displayed back on Boomerang.

https://4.bp.blogspot.com/-ISFaOcCxZvY/WQEfO_7AMCI/AAAAAAAACAE/H23ybgiJN-kGE93YXMkA5mG4rkGz2lL0ACLcB/s640/23.png

Delete Documents from DocumentDB via Logic Apps

Create a new logic app. This logic app will be used to delete documents from DocumentDB.

https://3.bp.blogspot.com/-jytm7eVB6P4/WQEfqu7QmoI/AAAAAAAACAI/8p5zq6StydUOa5GQrdYWkLnqAKpLFIO8QCLcB/s640/24.png

Trigger

Request: This trigger serves as an endpoint that you call via an HTTP Request to invoke your logic app. 

https://2.bp.blogspot.com/-DRiAB82dPVY/WQEf4snP7SI/AAAAAAAACAM/XvoEzO0yOskzDJ5yZqlLHayY9N-8FlBSACLcB/s640/25.png

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.

https://2.bp.blogspot.com/-RlLk-oR3qlY/WQEgjCkDV1I/AAAAAAAACAU/3mtnWHvgxTcgDHWKwMLBhgCeS72D33dpACLcB/s640/26.png

  • 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:

https://3.bp.blogspot.com/-R1ERgSul59Y/WQEgvESvukI/AAAAAAAACAY/WEaZ0ziBZ4Il3x0HknqPM7PiTru_Ii7lACLcB/s640/27.png

  • 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

https://2.bp.blogspot.com/-jENGNjBMFGk/WQEg-BZRbnI/AAAAAAAACAc/QXNBaw029lUX9WDttEbGOoS0Z29ncR6fQCLcB/s640/28.png

The logic apps run was successful and it was able to delete the document with the document id ‘abc2’ from the documentDb.

https://2.bp.blogspot.com/-DsCK2JVn_0Y/WQEhKTm1kHI/AAAAAAAACAg/FCDaCuWAMEcq0Uc_zg2M9ytVSJNDoktGwCLcB/s640/29.png

The response was displayed back on Boomerang.

https://2.bp.blogspot.com/-S1f1HN-ub1M/WQEhUPOef1I/AAAAAAAACAk/nE_TRQI5ITEXnPB1nO8aR_PDi0_at1u5ACLcB/s640/30.png

Moreover, the document was not visible anymore in the collection.

https://3.bp.blogspot.com/-Lz-mGUXcBBE/WQEhc7tNyOI/AAAAAAAACAo/YX8iope_7u086MdDxv-eLpC1oYC5UbFEgCLcB/s640/31.png