Query Firestore collection based on negation

岁酱吖の 提交于 2021-02-11 13:38:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!