I need to search a field that contains an array of the types of a bunch of different animals (eg. Bears, Cats, Dogs, Snakes, Raccoons). I’ll be searching for two or three animals each time (eg. Snakes and Raccoons". I’m familiar with the syntax for searching an array for a single animal, and when I do that it works fine:
db = Firestore.firestore()
let docRef = self.db.collection(“Animals")
let query = docRef.whereField("animal_types", arrayContains: “Rabbits")
//This works perfectly and returns all the records that contain rabbits
I see from the documentation that in order to search for multiple animals at a time I can use the arrayContainsAny operator, using the following syntax:
let query = docRef.whereField("animal_types", arrayContainsAny: [“Rabbits", “Raccoons"])
However, XCode is rejecting this operator, bringing up an error saying
No exact matches in call to instance method ‘whereField’
So my code can’t even compile. I updated all the Firestore cocoapods to the latest version in case my version was too old and didn’t have this feature, but no difference – the same error comes up every time. I’m using Swift 4 and XCode 11.6.
Edit: Here is a screenshot showing what happens when I try to enter either the arrayContainsAny operator or the in operator as was suggested – Xcode isn’t even recognizing them as valid values
As requested, here is the way my data is organized in Firestore. The Collection is called "Animals" and consists of documents with 2 fields – "animal_family" which is the scientific classification for the family the animal belongs to, and "animal_types" which will list the ordinary names for the types of animals that fall under that family. What I am trying to do is search the animal_types array field for multiple animal types (eg Rabbits and Raccoons as you see in my sample code) and have that return the matching documents.
2
Answers
After I double checked everything, it turns out the version of the FirebaseFirestore cocoa pod was too old. I thought I had updated them when I first had the problem, but turns out I had cd'd to the wrong folder and had run pod update on a backup copy of the project. Thanks to everyone who gave suggestions, as eliminating those potential issues led me back to checking on the basics again.
Google documentation show Swift syntax as:
https://firebase.google.com/docs/firestore/query-data/queries#swift_10