I'd like to perform a Firestore query to return an ordered list of blogs where the blog has an array that contains all elements of a query array.
An example is blog data such as this:
{
"cities": ["Tokyo", "Kyoto", "Osaka", "Nara"],
"modified": 1645457445
}
{
"cities": ["Prague", "Bratislava", "Budapest"],
"modified": 1645450245
}
{
"cities": ["Hiroshima", "Kyoto", "Tokyo"],
"modified": 1645453845
}
I want to get a page of 20 blogs that contains both "Tokyo" and "Kyoto", ordered by the most recently modified (descending).
I have seen the following questions
Firestore query - array contains all
How to perform compound queries with logical AND on array in Cloud Firestore?
When applying the above suggestions so the blog data looks like this:
{
"cities": {
"Hiroshima": true,
"Kyoto": true,
"Tokyo": true
},
"modified": 1645453845
}
And using the query
var db = admin.firestore();
var query = db.collection("blogs")
data.cities.forEach(tag => {
query = query.where("cities." + tag, "==", true)
})
query = query.orderBy("modified", "desc");
if (typeof data.modified !== "undefined") {
query = query.startAfter(data.modified)
}
return query.limit(20).get()
Firestore errors and requests to create an index such as:
cities.Tokyo Ascending modified Descending
The suggested index is not suitable as the blogs can contain any city in the world so it isn't feasible to create an index for every city
How can I achieve this query in firestore?