I have a collection of customers with their visited places, organised as follows:
{
"customer_id": 151,
"first_name": "Nakia",
"last_name": "Boyle",
"visited_places": [
{
"country": "Liberia",
"cities": [
"Mullerside",
"East Graham"
]
},
{
"country": "Rwanda",
"cities": [
"West Kristofer",
"Effertzbury",
"Stokeston",
"South Darionfort",
"Lewisport"
]
}
]
}
I am trying to find all customers that have visited a specific city in a specific country. I've got it working like this:
FOR target IN usertable
FILTER [] != target.visited_places[* FILTER CURRENT.country == @country AND CONTAINS(CURRENT.cities, @city)]
LIMIT @limit
RETURN target
The query seems cumbersome and I am not sure if it is performant.
Is there any better way to do this in terms of readability and performance?
You could filter by country and create a persistent array index for that on visited_places[*].country
but you still need a secondary condition that ensures that the country and city you look for occur in the same array element:
FOR doc IN usertable
FILTER @country IN doc.visited_places[*].country
FILTER LENGTH(doc.visited_places[* FILTER CURRENT.country == @country AND @city IN CURRENT.cities])
RETURN doc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.