[英]How to filter out certain documents in a firestore query
I'm looking to implement a friends search function in my social app.我希望在我的社交应用中实现朋友搜索功能。 For this I try to query for every user's document in the
users
collection of the DB, but it should exclude some specific documents like users who the current user is already friends with or request pending.为此,我尝试在数据库的
users
集合中查询每个用户的文档,但它应该排除某些特定文档,例如当前用户已经是朋友或请求待处理的用户。
Is it possible to query for all the users documents, excluding specific documents by passing those documents reference ID's in Firestore.是否可以通过在 Firestore 中传递这些文档引用 ID 来查询所有用户文档,不包括特定文档。
If not, can somebody say a way to structure the data so that I can implement this functionality.如果没有,有人可以说一种构建数据的方法,以便我可以实现此功能。 Thanks.
谢谢。
You can use the not-in
query.您可以使用
not-in
查询。 Here would be a simple example (taken from the docs):这是一个简单的例子(取自文档):
citiesRef.where('country', 'not-in', ['USA', 'Japan']);
Since you want to query by the ID, you can use firebase.firestore.FieldPath.documentId()
as the field name ( source )既然要通过ID查询,可以使用
firebase.firestore.FieldPath.documentId()
作为字段名( source )
citiesRef.where(firebase.firestore.FieldPath.documentId(), 'not-in', ['123', '456']);
Alternatively you might try to use the '__name__'
field-name ( source ):或者,您可以尝试使用
'__name__'
字段名称( source ):
citiesRef.where('__name__', 'not-in', ['123', '456']);
not-in
: https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any not-in
文档: https : //firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-anydocumentId
: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#static-documentid documentId
: https : //firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#static-documentid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.