title: How to perform dynamic whereField in FireStore queries?
body: <p>I have a dynamic search fields where users can enter up to 7 fields within each text field.
  <a href="https://i.stack.imgur.com/n5E2Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n5E2Q.png" alt="enter image description here" /></a></p>
  <p>According to firebase <a href="https://firebase.google.com/docs/firestore/query-data/queries" rel="nofollow noreferrer">documentation</a>, I can perform multiple queries as long as they are equality search. Is it possible to have a dynamic whereFields which increases based on the number of text fields searched. Each of these text fields represent a key in my Firestore that I can search equality for. I was able to store key pair values based on search text fields typed.</p>
  <p>Here's how I implemented a search query based on a static query. How can I write this line of code to support multiple fields based on the dictionary?</p>
  <pre><code>func fetchFavoriteCards(userId: String, completionHandler: @escaping (Result&lt;[CardData], Error&gt;) -&gt; Void) {
          database.collection(FirebaseDatabaseKeys.cards)
              .whereField(&quot;isFavorite&quot;, isEqualTo: true)
              .whereField(&quot;owner&quot;, isEqualTo: userId)
              .getDocuments { (querySnapshot, err) in
              if let error = err {
                  completionHandler(.failure(error))
              } else {
                  guard let documents = querySnapshot?.documents else {
                      print(&quot;No Cards&quot;)
                      return
                  }
  
                  let cards = documents.compactMap { (queryDocumentSnapshot) -&gt; CardData? in
  
                      do {
                          if let card = try queryDocumentSnapshot.data(as: CardData.self) {
                              return card
                          }
                      } catch {
                          print(&quot;FirebaseManager fetchFavoriteCards&quot;, error.localizedDescription)
                          return nil
                      }
                      return nil
                  }
                  completionHandler(.success(cards))
              }
          }
      }
  </code></pre>
  <p>I have tried this function below to first search for the very first key in the dictionary, then filter using high order functions to filter per property but I'm not sure this is the best route to move forward, I'm not sure if Swift also supports accessing dot notation based on string.</p>
  <pre><code>func fetchCardsWithSearchParameters(parameters: [String: Any], completionHandler: @escaping (Result&lt;[CardData], Error&gt;) -&gt; Void) {
  
      guard parameters.count &gt; 0,
            let initialKeyValue = parameters.first else {
          //completionHandler(.failure(SearchParameterError.emptyParameters))
          return
      }
  
      database.collection(FirebaseDatabaseKeys.cards)
          .whereField(initialKeyValue.key, isEqualTo: initialKeyValue.value)
          .getDocuments { (querySnapshot, err) in
          if let error = err {
              completionHandler(.failure(error))
          } else {
              guard let documents = querySnapshot?.documents else {
                  print(&quot;No Cards&quot;)
                  return
              }
  
              let cards = documents.compactMap { (queryDocumentSnapshot) -&gt; CardData? in
  
                  do {
                      if let card = try queryDocumentSnapshot.data(as: CardData.self) {
                          return card
                      }
                  } catch {
                      return nil
                  }
                  return nil
              }
              // filter based on the rest of the parameters
              for (key, value) in parameters {
                  cards.filter { $0.key == value }
              }
  
  
              completionHandler(.success(cards))
          }
      }
  }
  </code></pre>