Reputation: 155
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.
Is it possible to query for all the users documents, excluding specific documents by passing those documents reference ID's in Firestore.
If not, can somebody say a way to structure the data so that I can implement this functionality. Thanks.
Upvotes: 2
Views: 743
Reputation: 6747
You can use the not-in
query. 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)
citiesRef.where(firebase.firestore.FieldPath.documentId(), 'not-in', ['123', '456']);
Alternatively you might try to use the '__name__'
field-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-anydocumentId
: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#static-documentidUpvotes: 2