In the documentation it's stated that you can query by comparing string to another string value e.g:
citiesRef.where('name', '>=', 'San Francisco');
citiesRef.where('state', '>=', 'CA').where('state', '<=', 'IN');
Then there is this roles part in the documentation that shows how to apply roles in Firestore documents. However it's not shown how to query this.. But as shown in above example, this should work like following:
citiesRef.where(`roles.${user.uid}`, '>', '');
so this query above, should return all documents where is any value bigger than empty string, right?
In my code I have organizations collection with one document:
{
"name": "MyAmazingCompany",
"roles": {
"my-user-uid": "owner"
}
}
And if I try to query organizations where I have some role like this:
organizationsRef.where(`roles.${user.uid}`, '>', '');
I'll just get Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.
in my browser console (using firebase npm package version 5.1.0 and tried also 5.0.3).
Just to make sure that I should have access to that document, following query is tested, it works and it returns that one organization.
organizationsRef.where(`roles.${user.uid}`, '==', 'owner');
So what is wrong?
Also here is someone claiming it should work: Firestore select where is not null
And here are my rules:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
function getRole(rsc) {
// Read from the "roles" map in the resource (rsc).
return rsc.data.roles[request.auth.uid];
}
function isOneOfRoles(rsc, array) {
// Determine if the user is one of an array of roles
return isSignedIn() && (getRole(rsc) in array);
}
match /organizations/{organizationId} {
allow read: if isOneOfRoles(resource, ['owner']);
allow write: if isOneOfRoles(resource, ['owner']);
}
}
}
Like I said, it works if I compare if the role is owner, but I want to get results if user's uid exists in the roles array, no matter what role she is having.