Skip to main content


Showing posts from September, 2017

Populating CosmosDB Collection with Random values

Hi !!

Every time we need to populate a sample database, we have to start writing a tool again, because we forget where the previous tool is.  hahahahaha !!!!

It's true !!!
It has happened to me many times.

Ok, here is. Lets start !!

Step 1. Create Sample Data Base and Collection in CosmosDB

Step 2 Collection ready to insert random values

Step 3. Create C# Project Console in Visual Studio
Step 4. C# Code

Your EndPointURL and PrimaryKey are defined in Keys "Section" inside Cosmos DB. See image below. 

The following image illustrate some helpers methods to generate random values and the entity class definition.

Step 5.  Run example, results , queries
Query 1

Query 2

Code (Program.cs)

See you late !!!

Inserting Data with Azure Functions in CosmosDB

I was wondering how to use Azure Function to insert data in CosmosDB when this function receives an HTTP request
Surprise!!  It´s easy 
Step 1. Create your DataBase and Collection inside CosmosDB in Azure. 

Step 2. Create Function App

Step 3.  Create a new function. In this case use template "HttpTrigger - C#"

Step 4. Select "Integrate" option 

Be sure to change option in "Allowed HTTP methods" and select only "POST" HTTP method. 

Step 5. Create output binding

Step 6.  Configuring output binding

a:  Name of the entity variable to insert in CosmosDB. b:  Name of the collection where the entity will be inserted (see image in Step 1). c:  Partition Key  in case you need to define it.  d:  DataBase name in Cosmos DB (see image in Step 1). e:  ConnectionString.

Step 7.   "Short" programming....
a: Entity variable referenced in Step 6. a b: Entity type (custom class). 
outputDocument= req.Content.ReadAsAsync<DTODocument>().Result;
The functio…

I just wanted to remember him

It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong.
Richard P. Feynman