We have a below document structure.
{
"id":"GUID",
"customer": {
"contacts": [
{
"type": "MOBILE",
"status": "CONFIRMED",
"value": "xxxx"
},
{
"type": "EMAIL",
"status": "CONFIRMED",
"value": "aaaa"
}
],
"addresses": [
{
"country": "xxx"
}
]
}
}
and need to search for customer->contacts where value="aaaa".
I tried with below options
1) SELECT c.id FROM c
join customer in c.customer
join contacts in c.customer.contacts
where contacts.value = "aaaa"
2) SELECT c.id FROM c WHERE c.customer.contacts[0].value= "aaaa"
Getting Syntax error 400 bad request Any help highly appreciated
Part of your issue is that value cannot be searched as easily as other properties. Here are possible solutions:
SELECT c.id FROM c
join contacts in c.customer.contacts where contacts["value"] = "aaaa"
SELECT c.id FROM c WHERE c.customer.contacts[1]["value"] = "aaaa"
Document DB SQL Api - unable to query json property with name 'value' and its value is integer
Related
I'm trying to find accounts with a "extra-storage" premium package for a particular user in my Azure CosmosDb database. Here's what my Account object looks like:
{
"id": 123,
"name": "My Account",
"members": [
{
"id": 333,
"name": "John Smith"
},
{
"id": 555,
"name": "Jane Doe"
}
],
"subscription": {
"type": "great-value",
"startDate": "2022-04-21T16:38:00.0000000Z",
"premiumPackages": [
{
"type": "extra-storage",
"status": "active"
},
{
"type": "video-encoding",
"status": "cancelled"
}
]
}
}
So, my conditions for the query (in-English) are:
Account must contain "John Smith" (id: 333) as a member
It must have the "extra-storage" premium package in its subscription
I'm not sure if I can have multiple JOINs but here's what I've tried with no results so far:
SELECT c.id, c.name, s.premiumPackages.status
FROM c JOIN m IN c.members
JOIN s IN c.subscription
WHERE CONTAINS(m.id, 333)
AND CONTAINS(s.premiumPackages.type, "extra-storage")
Any idea how I can get accounts with "extra-storage" package for "John Smith"?
This query should give you what you are looking for.
SELECT c.id, c.name, premiumPackages.status
FROM c
JOIN (SELECT VALUE m FROM m IN c.members WHERE m.id = 333)
JOIN (SELECT VALUE s FROM s IN c.subscription.premiumPackages WHERE s.type
= "extra-storage") AS premiumPackages
This blog post on Understanding how to query arrays in Azure Cosmos DB is helpful to keep bookmarked when trying to write queries for arrays.
PS, id on the root in Cosmos DB must be a string so your "id": 123 should be "id": "123".
I'm trying to run a query in CosmosDB but I can't seem to get the result the way I want it.
This is the query:
SELECT c FROM c
JOIN p in c.Data.packages
WHERE p.packageId ="ID_9DACF11F-31F1-45C0-9A99-7ED846F9226E"
Is there a way to get the following result without the "c":
[
{
"c": {
"id": "ID-6A23-432D-B862-4342D6B8C6F0",
"prop1": "value",
"prop2": "value",
"Data": {
"date": "2020-01-30T18:21:57",
"packages": [
{
"packageId": "123"
}
]
}
}
}
]
As in just:
[
{
"id": "ID-6A23-432D-B862-4342D6B8C6F0",
"prop1": "value",
"prop2": "value",
"Data": {
"date": "2020-01-30T18:21:57",
"packages": [
{
"packageId": "123"
}
]
}
}
]
I know I can use c.id, c.prop1, etc. But I have tons of more properties, and it will get really hard to maintain in the future. So is there a way to achieve this?
Basically looking for something like this:
SELECT c.* FROM c
JOIN p in c.Data.packages
WHERE p.packageId ="ID_9DACF11F-31F1-45C0-9A99-7ED846F9226E"
But this doesn't work obviously. Any help appreciated!
For queries with multiple aliases use VALUE c instead.
SELECT VALUE c FROM c
JOIN p in c.Data.packages
WHERE p.packageId ="ID_9DACF11F-31F1-45C0-9A99-7ED846F9226E"
Docs: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-select
The following json represents two documents in a Cosmos DB container.
How can I write a query that gets any document that has an item with an id of item_1 and value of bar.
I've looked into ARRAY_CONTAINS, but don't get this to work with array's in array's.
Als I've tried somethings with any. Although I can't seem to find any documentation on how to use this, any seems to be a valid function, as I do get formatting highlights in the cosmos db explorer in Azure Portal.
For the any function I tried things like SELECT * FROM c WHERE c.pages.any(p, p.items.any(i, i.id = "item_1" AND i.value = "bar")).
The id fields are unique so if it's easier to find any document that contains any object with the right id and value, that would be fine too.
[
{
"type": "form",
"id": "form_a",
"pages": [
{
"name": "Page 1",
"id": "page_1",
"items": [
{
"id": "item_1",
"value": "foo"
}
]
}
]
},
{
"type": "form",
"id": "form_b",
"pages": [
{
"name": "Page 1",
"id": "page_1",
"items": [
{
"id": "item_1",
"value": "bar"
}
]
}
]
}
]
I think join could handle with WHERE clause with array in array.Please test below sql:
SELECT c.id FROM c
join pages in c.pages
where array_contains(pages.items,{"id": "item_1","value": "bar"},true)
Output:
{
"id": "3d50809d-d631-4576-925a-7232d9ef9338",
"TrackingId": "3d50809d-d631-4576-925a-7232d9ef9338",
"records": [
{
"measurementTime": {
"value": "2018-04-01 10:00:00.000",
"unit": "datetime"
},
"systolic": {
"value": 114,
"unit": "mm(hg)"
},
"diastolic": {
"value": 88,
"unit": "mm(hg)"
}
}
]
},
{
"id": "c5f3bd10-1959-4407-92cb-6d9548950f2c",
"TrackingId": "c5f3bd10-1959-4407-92cb-6d9548950f2c",
"records": [
{
"measurementTime": {
"value": "2018-04-02 10:00:00.000",
"unit": "datetime"
},
"systolic": {
"value": 122,
"unit": "mm(hg)"
},
"diastolic": {
"value": 91,
"unit": "mm(hg)"
}
}
]
}
I am trying to querying documents in Azure Docuemnt DB having this sample format. (please don't try to validate JSON or property names in query, i am giving just a stripped of version to understand hierarchy)
I am successfully querying some properties, but i am getting error when trying to fetch the integer value under systolic--> value or diastolic--> value with the query mentioned below.
Success Query:
SELECT
c.id,
c.TrackingId,
c.records[0].measurementTime["value"]
Failure Query:
SELECT
c.id,
c..TrackingId,
c.records[0].measurementTime["value"],
c.records[0].systolic["value"],
c.records[0].diastolic["value"]
the keyword usage of ["value"] is working for measuremenTtime but not working for systolic or diastolic
Error details:
Message : "Object creation error, property name 'value' specified more than once.
code:400
severity: Error
Kailash Ravuri, in fact, your issue is not related to field name or field type. Based on your query sql, your result data have 3 fields named 'value',it's not allowed. You just need to add an alias to 3 fields and everything will be ok.
SELECT
c.id,
c.TrackingId,
c.records[0].measurementTime["value"] as measurementTimeValue,
c.records[0].systolic["value"] as systolicValue,
c.records[0].diastolic["value"] as diastolicValue FROM c
Hope it helps you.
Let's assume that I have the following document:
[
{
"name" : "obj1",
"field": [ "Foo1", "Foo3" ]
},
{
"name": "obj2",
"field": [ "Foo2" ]
},
{
"name": "obj3",
"field": [ "Foo3" ]
},
{
"name": "obj4",
"field": [ "Foo1" ]
}
]
I want to write a query which returns obj1, obj3, and obj4 when field = "Foo1" or "Foo3" are searched for. Obviously I can write something like:
SELECT * FROM c WHERE ARRAY_CONTAINS(c.field, "Foo1") OR ARRAY_CONTAINS(c.field, "Foo3")
Though I want to avoid constructing a long query by concatenating query string with ARRAY_CONTAINS for each value in search list.
How can this query be expressed succinctly?
You could rewrite the query using JOIN as follows:
SELECT c
FROM c
JOIN tag IN c.field
WHERE ARRAY_CONTAINS(["Foo1", "Foo3"], tag)
Note that if you have an object with both tags, then it would occur multiple times in the result, and you have to perform distinct/de-duping on the client side.