1

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.

MauriceNino
  • 6,214
  • 1
  • 23
  • 60
Nawaz Mohamed
  • 145
  • 10

1 Answers1

2

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']);
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • Thanks, with array not-in documentation says that for single query you can only compare 10 values, so if it more than 10 is there is any way other than use multiple queries. – Nawaz Mohamed Jun 22 '21 at 08:41
  • @NawazMohamed Can't think of one, but you could certainly automate the creation of that compound query, so the downsides aren't that big. – MauriceNino Jun 22 '21 at 08:48
  • Hi, is it not possible to use fieldPath with orderBy, it returns error `'FirestoreFieldPath' cannot be used in conjunction with a different Query.orderBy() parameter` – Nawaz Mohamed Jun 22 '21 at 09:32
  • @NawazMohamed seems so - maybe open another question for that, as it is not included in your original question. – MauriceNino Jun 22 '21 at 09:38