问题
We are developing a messaging system where the users are able to block each other. This means we need the ability to query message where the author is not blocked.
Pseudo: Select * from messages where author_id not in {list_of_blocked_ids}
Since Firestore does not support array_not_contains or any other negation-methods we are quite unsure how to structure this data to be able to make this query.
Any help structuring this information about blocked users is appreciated.
回答1:
To solve this issue, according to the official documentation regarding Query limitations:
Cloud Firestore does not support the following types of queries:
Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).
So with other words, there is no !=
(not equal to) operator in Firestore nor a arrayNotContains()
function. The only option that you have is to split your query into a greater-than
and a less-than
query and then it will work perfectly fine.
回答2:
In each "conversation" document, put subcollections - one called "messages" and one called "blocklist". Assign the blocked users uid
as key to boolean value of true
. Use Firestore Rules to enforce the blocking, rather than a query.
PSEUDO: allow read, write if this uid is not found in this conversations blocklist
来源:https://stackoverflow.com/questions/52831223/query-firestore-collection-based-on-negation