Skip to main content

Querying CosmosDB. Part 2

Hi!!!

We will continue to play a little with the same JSON Example in Part 1

We will try to do queries a little more complicated.



Total amount of time (in minutes) of laboratory in particular event.

Query

SELECT VALUE SUM(act.duration) FROM c
JOIN act IN c.activities
WHERE act.type=”lab” AND c.event_name= “CosmosDB Conf”


Results




List activities from specific type and from specific event

Query

SELECT VALUE act FROM c
JOIN act IN c.activities
WHERE act.type=”conference” AND c.event_name= “CosmosDB Conf”


Results



Get activities where duration is more than 30 minutes

Query

SELECT VALUE act FROM c JOIN act IN c.activities
WHERE act.duration>30


Results



Same as previous but bring me more information for each document

Query

SELECT VALUE { “id”:c.id,
“event”:c.event_name,
“speaker”:c.speaker,
“country”:c.event_country,
“activity”:act} FROM c
JOIN act IN c.activities
WHERE act.duration>30 and act.type=”lab”


Results




In case you have more complex JSON Document let us know. I’d like to help and be involve in other queries problems.


To be Continue…

Comments

Popular posts from this blog

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…

Update data in COSMOS DB with Azure Functions.

I was looking for a way to update COSMOS DB document after receive a  request inside Azure Function without any COSMOS DB biding.

I didn't find a way !!
 I took another way. I don't know if it is the right or wrong way. But this way works for me.

Scenario is very simple. We have sales orders arriving into the data base. We have another Azure function to insert those new orders. But suppose we need to cancel some order. How to do that using Azure Function ?

Let´s start !!!
Step 1. Sample Collection. This collection is very simple,  we are focused on results.


Step 2. Create a Function and Select HttpTrigger template. 


Step 3. Code 


More code. 

Step 4. Test

Step 5. Results 




See you soon 

Querying CosmosDB. Part 1

Hi CosmosDB Community !!!
I'd like to contribute with some posts about how to query Cosmos DB Collection with SQL API.

Suppose we have a speaker list of differents events around a whole world.

The link above represent speaker list example.
Example1.json

For all examples we will use Data Explore in Azure Portal. Anyway, you can use any API in any supported programming language to write your queries. We are not using Modeling data techniques or witting same data in different ways to avoid cross partition queries. The main objective of the post is how to use built-in functions and operators in CosmosDB SQL API.

I'd like to list all events from Hassan Arteaga Rodríguez 
Query
SELECT * FROM c where c.speaker = "Hassan Arteaga Rodriguez"
Results




I need to list all events from MEXICO. Not all fields Query
SELECT c.speaker, c.event_name,c.init_date,c.end_date FROM c where c.event_country = "MEXICO"
Results

I'd like to list all events  in June Query

SELECT * FROM c…