简体   繁体   中英

Swift firebase firestore query

var selectedColor: String = "pink"
var selectedScore: String = "-"

let pillsRef = db.collection("pills")
    pillsRef
      .whereField("pillColor", isEqualTo: "pink")
      .whereField("pillColor", isEqualTo: "blue")
      .getDocuments() { (querySnapshot, err) in

    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        print("\(document.documentID) => \(document.data())")
    }
}

I have a question for this code.

If I compile this code, it will be error.

I want to retrieve data contained "pink" or "blue".

How can I do this?

Cloud Firestore does not support: Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

So you need to do the first query for first color and then do another query for second color and then merge the results:

pillsRef
  .whereField("pillColor", isEqualTo: "pink")
  .getDocuments() { (document, err) in
      if let err = err {
          print("Error getting documents: \(err)")
      } else {
          print("\(document.documentID) => \(document.data())")
          pillsRef.whereField("pillColor", isEqualTo: "blue").getDocuments() { (document2, err2) in
             if let err2 = err2 {
                print("Error getting documents: \(err)")
             } else {
               // here you get your data for blue color 
               print("\(document2.documentID) => \(document2.data())")
             }
         }
     }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM