0

Trying to grab all documents where array does not contain user uid.

However function keeps returning all documents... Am i missing something?

 func grabDocuments() {
        guard let userProfile = UserService.currentUserProfile else { return }
        print(userProfile.uid)
        Firestore.firestore().collection("all").whereField("group", notIn: [userProfile.uid])
            .addSnapshotListener { (querySnapshot, err) in
                
                if err == nil && querySnapshot != nil {
                    if querySnapshot!.documents.count > 0 {
                        self.dataExists()
                        for document in querySnapshot!.documents {
                            let data = document.data()
                            let group = data["group"] as? Array<String> ?? [""]
                            print("---------------------------")
                            print(group)
                            print("---------------------------")
                        }
                    }
                }
            }
    }

printed output

liwIbfyZjUSVcXbhhxQUMR1fQ4p2
---------------------------
["RRPtSbWZTEYKgBVQbxpCJROOcQ33"]
---------------------------
---------------------------
["RRPtSbWZTEYKgBVQbxpCJROOcQ33"]
---------------------------
---------------------------
["RRPtSbWZTEYKgBVQbxpCJROOcQ33"]
---------------------------
---------------------------
["RRPtSbWZTEYKgBVQbxpCJROOcQ33"]
---------------------------
---------------------------
["RRPtSbWZTEYKgBVQbxpCJROOcQ33", "hl0RVRbfw4RmQAtSuIO5dTB6doF3", "liwIbfyZjUSVcXbhhxQUMR1fQ4p2"]
---------------------------
---------------------------
["hl0RVRbfw4RmQAtSuIO5dTB6doF3", "RRPtSbWZTEYKgBVQbxpCJROOcQ33", "liwIbfyZjUSVcXbhhxQUMR1fQ4p2"]
---------------------------

Firestore documentation

https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any

citiesRef.whereField("country", notIn: ["USA", "Japan"])

To be clear. Getting all documents where array does exists works as expected.

Firestore.firestore().collection("all").whereField("group", arrayContains: userProfile.uid)
Redstain
  • 157
  • 2
  • 10
  • We can't see the documents in your database, and we can't see the value of `userProfile.uid`, so it's impossible for us to tell what this query is actually going to do. Please edit the question to explain more clearly what is not working the way you expect along with your debugging details. – Doug Stevenson Jun 13 '23 at 15:30
  • Updated my question. Now you can see the printed output from function. I also want to point out that doing the opposite with "arrayContains" works as expected. I'ts only when i do notIn that all documents get returned. – Redstain Jun 13 '23 at 15:47
  • 1
    You're printing `userP.uid` but using a different value `userProfile.uid` in the query. – Doug Stevenson Jun 13 '23 at 15:58
  • Sorry, corrected Typo. Rewrote variable name for better readability. – Redstain Jun 13 '23 at 16:00
  • 1
    Instead of describing what your database looks like, please edit your question and add a screenshot of it. What is the value of `userProfile.uid`? Please respond using @AlexMamo – Alex Mamo Jun 14 '23 at 08:54
  • @AlexMamo I found a similar question after digging for a while. I assume many people thought that the "notIn" would function as a "array-does-not-contain" but is actually different https://stackoverflow.com/questions/74332564/firebase-operator-not-in-is-not-working – Redstain Jun 14 '23 at 11:41
  • Did the [Link](https://stackoverflow.com/questions/74332564/firebase-operator-not-in-is-not-working) you provided answered the similar question worked for you? – DominicT Jun 16 '23 at 19:32
  • 1
    @DominicT Yes, it basically explained that the notIn query is not used for arrays but for a Fieldvalue. I guess its placement in the documentation made it confusing for me and others. – Redstain Jun 17 '23 at 05:41

1 Answers1

1

Publishing this as community wiki for other's sake.


As seen in this similar question by @Redstain link, that the notIn query is not used for arrays but for a Fieldvalue.


You can check this link for other references:

DominicT
  • 388
  • 1
  • 9