I am new to cosmos db and I have done a lot of SQL. I would like to know how can I query cosmos collection properties that is an array.
For example:
Cosmos Data
"id": "123",
"addressArray": {
"date": null,
"type": "Home",
"addresses": [
{
"type": "ALL",
"city": "London"
},
{
"type": "City",
"city": "Paris"
},
{
"type": "City",
"city": "New York"
}
],
"Use": null
}
Now I want to write a query in cosmos to find addresses in Paris and New York where Type is city
Select * from c where c.addressArray.addresses[0] = "New York"
What should be the query when there is an array?
You need this?
SELECT value city
FROM Games g
JOIN city IN g.addressArray.addresses
WHERE city.city = 'New York' OR city.city='Paris'
Related
Given the following collection data in a CosmosDB database:
[
{
"id": "1",
"title": "Blog post #1",
"tags": ["tag1", "tag2", "tag3"]
},
{
"id": "2",
"title": "Blog post #2",
"tags": ["tag1"]
}
]
How would you flatten this into a result set that looks like the following:
[
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag1"
},
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag2"
},
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag3"
},
{
"id": "2",
"title": "Blog post #2",
"tagName": "tag1"
}
]
The ultimate goal in this example is finding the tag count, so this query would be wrapped in something like SELECT COUNT(1) as count, s.tagName FROM (subquery) s GROUP BY s.tagName.
You got the right idea but are using the wrong type of join. You need to use JOIN IN to flatten the array.
SELECT c.id, c.title, t AS tagName
FROM c
JOIN t IN c.tags
Query: I wanted to get the list of records for all people born in the month of March.
{
"details": {
"state": "CA",
"city": "San Fransisco",
"date-of-birth": { // there is a "-" in the key
"month": "March",
"year": "2000"
}
},
"personId": "person1",
"id": "id1"
},
{
"details": {
"state": "CA",
"city": "San Jose",
"date-of-birth": { // there is a "-" in the key
"month": "April",
"year": "2000"
}
},
"personId": "person2"
"id": "id2"
}
I was hoping that the SQL Query would be like this, but got an error :
select * from c where c.details['date-of-birth'['month']] = "March"
Can someone help me out with the query? I did try to look at the docs but got a little bit confused.
Try this
select * from c where c.details['date-of-birth'].month = "March"
with big hope asking this question.geeks please suggest..
I have huge data stored into cassandra, with all US cities, state,lat and long. what would be the cql query to get lat and long of multiple city in multiple state.
sample req
{
"city": "Northbrook",
"state": "IL"
},
{
"city": "Chicago",
"state": "IL"
},
{
"city": "Schaumburg",
"state": "IL"
},
{
"city": "Omaha",
"state": "NE"
},
{
"city": "Bellevue",
"state": "NE"
},
{
"city": "Paige",
"state": "AZ"
}
Query used : select * from tablename where city in ('Northbrook','Paige','Chicago','Bellevue','Omaha','Schaumburg') and state in ('IL','AZ','NE')
problem is, it's not returning all six cities lat and long and other details. Can anyone help me here ?
I am using Node/Express/Mongo/Mongoose/jQuery/jQuery UI Auto-complete. I have a collection of Businesses that look like this:
[
{
name: "Example Corporation",
"address": {
"city": "New York",
"state: "NY"
},
{
name: "ABC Corp",
"address": {
"city": "Atlanta",
"state: "GA"
},
{
name: "ACME Inc",
"address": {
"city": "Dallas",
"state: "TX"
},
{
name: "New York Company",
"address": {
"city": "New York",
"state: "NY"
},
{
name: "XYZ Co",
"address": {
"city": "Newark",
"state: "NJ"
}
}
]
I want to create an auto-complete that would allow someone to type in the partial name of the business name OR city and get results back grouped like so:
If I search "New" I want:
{
businesses: [
{
name: "New York Company",
address: {
"city": "New York",
"state": "NY"
}
},
{
name: "XYZ Co",
address: {
"city": "Newark",
"state": "NJ"
}
}
],
cities: [
{
"city": "New York",
"state": "NY"
},
{
"city": "Newark",
"state": "NJ"
}
]
}
Very similar to an auto-complete search you might see on OpenTable where you can search by restaurant or location. I want unique city names returned.
Do I need to search the collection twice per business name and city and tie it all together at the end? I'm so confused. I don't need ID's back since the end result they either go to a City page or the Business page. I hope this makes sense.
My document in DocumentDb looks like this:
{
"id": 123,
"timers":
{
"projectTimer":
{
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
},
"taskTimer":
{
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
}
The key points here are:
"timers" is an object -- NOT an array
The sub-objects are also set i.e. "projectTimer" and "taskTimer"
If I set my SELECT statement to the following, it works by giving me both projectTimer and taskTimer sub-objects
SELECT c.timers
FROM Collection c
WHERE c.id = 123
But the following returns nothing. I don't understand why because it seems like a really simple JOIN:
SELECT t.projectTimer
FROM Collection c
JOIN t IN c.timers
WHERE c.id = 123
Any idea where I'm making a mistake?
The issue is that you're trying to do a JOIN on something that's not an array.
If, instead, you reworked your document slightly:
{
"id": "123",
"timers": [
{
"projectTimer": {
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
}
},
{
"taskTimer": {
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
],
}
You'd then be able to do a JOIN like:
select value t
from collection c
join t in c.timers
where c.id = "123"
Which would return each of the timers in the array:
[
{
"projectTimer": {
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
}
},
{
"taskTimer": {
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
]
Note the use of VALUE in the query, to strip away the containing t variable.