Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
- An Azure Cosmos DB account, database, and container. If you don't have these resources, see 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 Queries in Azure Cosmos DB for NoSQL.
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"
}
]