Reputation: 67
I want to present a list of object in my web app but these objects came from different queries because firestore doesn't support "or condition" it just support it with arrays in "array_contains_any". Then, I need to retrieve that information from two queries and I would like to merge the queries on a single one but I don't know if it is possible. On the other hand,I think I could paginate it separately but I can't do sorting properly because It will order my documents separately that's why I want to concatenate the queries on a single one. I'd greatly appreciate your help.
These are the queries:
query1 = coll_ref.where(fieldname,u'==',value)
query2 = coll_ref.where(u'fieldname',u'array_contains',value)
I would like to:
query.sort(fieldname).limit(3)
Thank you for your time
Upvotes: 1
Views: 45
Reputation: 580
I see a potential solution to be to run query1 and query2, sort them by fieldname, and limit them by three, then in your frontend code, sort those responses by fieldname and return the first three. For example (pseudocode):
query1results = coll_ref.where(fieldname,u'==',value).sort(fieldname).limit(3)
query2results = coll_ref.where(u'fieldname',u'array_contains',value).sort(fieldname).limit(3)
combined = query1results.concatenate(query2results).unique()
combined.sort(fieldname)[:3]
I am not sure what your language is, so I can't give you an exact implementation. In the above example, lets says that query1results
returns ['apple', 'banana', 'orange']
and query2results
returns ['banana', 'cucumber', 'kiwi']
, the result of query1results.concatenate(query2results).unique
would be ['apple', 'banana', 'orange', 'cucumber', 'kiwi']
, then sort and limit three so the result would be ['apple', 'banana', 'cucumber']
which is equivalent to query.where(query1 or query2).sort(fieldname).limit(3)
Upvotes: 1