Home > Software engineering >  How can I get only the users with roles of 'user'?
How can I get only the users with roles of 'user'?

Time:10-17

I am using Firebase for this app. Under each documents of a user, I have this array roles. For normal users, their roles would just be user. For an admin, it would be user and admin. I want to retrieve all of the normal users. However, the problem is that if I'll set it like this:

.collection("users")
.where("roles", "!=", "admin")

this will still show the user with an admin role.

enter image description here

How can I retrieve only those normal users and not the user with an admin role?

CodePudding user response:

This would require an array-does-not-contain operation, which does not exist in Firestore.

You might want to consider storing the roles as a map with boolean values, so that you can query for where("roles.admin", "==", false).

Also see:

CodePudding user response:

You can use 'not-in' roles not in admin then you will get normal user

docs: https://firebase.google.com/docs/firestore/query-data/queries

  • Related