Tutorial: Query data in Azure Cosmos DB for NoSQL
APPLIES TO: NoSQL
Azure Cosmos DB for NoSQL supports querying documents using the built-in query syntax. This article provides a sample document and two sample queries and results.
This article covers the following tasks:
- Query NoSQL data with the built-in query syntax
Prerequisites
This tutorial assumes you have an Azure Cosmos DB account, database, and container.
Don't have any of those resources? Complete this quickstart: Create an Azure Cosmos DB account, database, container, and items from the Azure portal.
You can run the queries using the Azure Cosmos DB Explorer in the Azure portal. You can also run queries by using the REST API or various SDKs.
For more information about queries, see getting started with queries.
Sample document
The queries in this article use the following sample document.
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female", "grade": 1,
"pets": [
{ "givenName": "Goofy" },
{ "givenName": "Shadow" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 8
}
],
"address": { "state": "NY", "county": "Manhattan", "city": "NY" },
"creationDate": 1431620462,
"isRegistered": false
}
Select all fields and apply a filter
Given the sample family document, the following query returns the documents where the ID field matches WakefieldFamily
. Since it's a SELECT *
statement, the output of the query is the complete JSON document:
Query:
SELECT *
FROM Families f
WHERE f.id = "WakefieldFamily"
Results:
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female", "grade": 1,
"pets": [
{ "givenName": "Goofy" },
{ "givenName": "Shadow" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 8
}
],
"address": { "state": "NY", "county": "Manhattan", "city": "NY" },
"creationDate": 1431620462,
"isRegistered": false
}
Select a cross-product of a child collection field
The next query returns all the given names of children in the family whose ID matches WakefieldFamily
.
Query:
SELECT c.givenName
FROM Families f
JOIN c IN f.children
WHERE f.id = 'WakefieldFamily'
Results:
[
{
"givenName": "Jesse"
},
{
"givenName": "Lisa"
}
]
Next steps
In this tutorial, you've done the following tasks:
- Learned how to query using the built-in query syntax
You can now proceed to the next tutorial to learn how to distribute your data globally.