0

how to query users that are not followed by the user in firestore? firebase structure:

firestore (root)
  ----users (colloection)
      ----user (doc)
         uid: "xyz"

  ----following (collection)
      ---- current users uid (doc)
         ----- following (collection)
              --- user followed (doc)
          ---- followers (collection)
              ---- users following (doc)

I am building people recommended to follow like facebook

And i want to query users that are not followed by me, is that possible, if so pls answer with the js query snippet?

I am using: firebase web 9

Arnav Singh
  • 194
  • 2
  • 4
  • 2
    There is no way in Firestore to query for documents without a certain piece of information. – Frank van Puffelen Apr 27 '22 at 20:20
  • The [`not-in`](https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any) operator is probably what you are looking for but you'll need to know IDs of users following a user. However `not-in` only combines up to 10 not equal (!=) clauses. e.g. `where(documentId(), "not-in", [follower1,...])` – Dharmaraj Apr 27 '22 at 20:20
  • I don't think `not-in` would work here @Dharmaraj. The `not-in` checks whether a specific value in the document is not equal to a number of other values. From what I can tell, there is no value in the document to search for here, so the document won't be in the index for the relevant field/values. – Frank van Puffelen Apr 27 '22 at 20:22
  • Yes I meant if there's any way to get an array of followers' UIDs (from followings sub-collection) then a query on users collection might be useful (to some extent because the array size can be max 10) which would return users not following by given user in best case. – Dharmaraj Apr 27 '22 at 20:25
  • @FrankvanPuffelen how do i exclude the users followed in the query like this const _query = query(usersQuery, where() // <-- what should i add here in where clause, orderBy("createdAt", "desc"), limit(_limit) ); ``` – Arnav Singh Apr 27 '22 at 20:29
  • @ArnavSingh As said: this type of query is not possible on Firestore, as the items that you are looking for **don't** have a value so they aren't in the index. Check the question I linked, and the links from there for a longer explanation and some alternative data model (but I'm not sure if any apply here). – Frank van Puffelen Apr 27 '22 at 21:48

0 Answers0