I'm playing around on https://www.documentdb.com/sql/demo, which allows me to query against sample documents that look like:
{
"id": "19015",
"description": "Snacks, granola bars, hard, plain",
"tags": [
{
"name": "snacks"
}
],
"version": 1,
"isFromSurvey": false,
"foodGroup": "Snacks",
"servings": [
{
"amount": 1,
"description": "bar",
"weightInGrams": 21
}
]
}
I'm confused about ARRAY_CONTAINS()
. This query returns results:
SELECT root
FROM root
WHERE ARRAY_CONTAINS(root.tags, { "name": "snacks" })
However, this query does not:
SELECT root
FROM root
WHERE ARRAY_CONTAINS(root.servings, { "description": "bar" })
What gives?
What I'm trying to achieve is illustrated by how I would write the query if this was C#:
var filteredDocs = docs.Where(d => d.Servings != null &&
d.Servings.Length > 0 &&
d.Servings.Any(s => s.Description == "bar"));
It appears the first example query on root.tags
works because { "name": "snacks" }
is the entire object in the root.tags
array, while, in the second query, { "description": "bar" }
is only one field in the root.servings
objects.
How can I modify the second query on root.servings
to work with only knowing the serving description
?
The Azure Cosmos DB API for NoSQL supports querying documents using SQL.
CosmosDB is the new DocumentDB for NoSQL solution. The Azure Cosmos DB DocumentDB API or SQL (DocumentDB) API is now known as Azure Cosmos DB SQL API. You don't need to change anything to continue running your apps built with DocumentDB/DocumentDB API.
Add data to your databaseIn Data Explorer, expand the ToDoList database, and expand the Items container. Next, select Items, and then select New Item. Select Save. Select New Item again, and create and save another document with a unique id , and any other properties and values you want.
A self-link is a static addressable Uri for each resource within a database account and follows the Azure Cosmos DB resource model.
Note that this is a workaround that works fine.
SELECT c.id FROM c JOIN a in c.companies where a.id = '{id}'
In Linq this would be
x => x.companies.Any(z => z.id == id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With