0

I want to query search of multiple collections, not just brand. I query directly from cloud firestore

so far I successfully query the brand.

I try to combine also with category and description, but it always fails

I use where() for that to call directly from firebase

getExchanges({commit, state}, {searched} = {searched: ''}) {
      commit('resetPagination');
      // Here you want to make a call to firebase and ask for data
      return db
       .collection('exchanges')
       .where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')
       .limit(state.pagination.itemCount)
       .get()
       .then(snapshots => {
         if (snapshots.docs.length === 0) {
            commit('setExchanges', []);
            return []
         }
         const exchanges = snapshots.docs.map(doc => ({...doc.data(), id: doc.id}))
         commit('setExchanges', exchanges)
         commit('setLastItem', snapshots.docs[snapshots.docs.length - 1])
         commit('setPreviousFirstItem', snapshots.docs[0])
         return exchanges
        })

My field in the exchange collection in the firestore is

  • brand
  • category
  • city

With category, i try to do this

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

Do you have some suggestions? that would be amazing

Cheers

Zaid

alfa zaid
  • 159
  • 1
  • 1
  • 5
  • It looks like you're trying to do a logical OR query, which is not supported by Firestore. – Doug Stevenson Sep 06 '20 at 06:04
  • You may be interested by this article: https://medium.com/firebase-tips-tricks/how-to-combined-two-firestore-queries-to-simulate-a-logical-or-query-27d28a43cb2d *(disclaimer: I'm the author)* – Renaud Tarnec Sep 06 '20 at 10:20

1 Answers1

1

Currently Cloud Firestore does not support "OR" queries. The closest thing you can have is using the "array-contains" and "array-contains-any" queries and explicitly design your data structure to suit the query needs. An alternative would be to use a third party service called Algolia.

Another point to note is that your code

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

is identical to

.where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')

as || operator in javascript returns the second term if the first term is undefined, null, an empty string, an empty array or false and returns the first term otherwise.

CRUD DS
  • 456
  • 2
  • 5
  • Hi Thank you very much for the suggestion I will try to use the array-contains-any Thank you for OR information, now i relazie why it keeps failing cheers Zaid – alfa zaid Sep 06 '20 at 06:11
  • hi CRUD DS It seems that array-contains-any is not for multiple fields, it is used for multiple value? – alfa zaid Sep 06 '20 at 06:33