How can I retrieve the id of a document I added to a Cosmosdb collection?

Author: horatius

Originally Sourced from: https://stackoverflow.com/questions/49357318/how-can-i-retrieve-the-id-of-a-document-i-added-to-a-cosmosdb-collection

I have a single collection into which I am inserting documents of different types. I use the type parameter to distinguish between different datatypes in the collection. When I am inserting a document, I have created an Id field for every document, but Cosmosdb has a built-in id field.

How can I insert a new document and retrieve the id of the created Document all in one query?