Nawaz Mohamed
Nawaz Mohamed

Reputation: 155

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.

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

Answers (1)

MauriceNino
MauriceNino

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']);

Upvotes: 2

Related Questions