0

I have a collection of users, which looks like this:

enter image description here

Each follower/following document is structured like this:

{
  uid: ...,
}

I want to create a query that satisfies these criteria:

  1. Get the users that you are following
  2. Ordered by their 'lastPosted' time (so you're getting the users who have posted most recently first)

Since the follower/following subcollections only contain references to full user documents, and not the full documents themselves, it's not as easy as just querying the following subcollection.


This is what I've tried:

database
.collection('users')
.doc(myUid)
.collection('following')
.get()
.then(followings => {
  database
  .collection('users')
  .orderBy('lastPosted', 'desc')
  .where('uid', '==', following[0] || following[1] ...)
})

Which doesn't work. I have also thought about this:

database
.collection('users')
.orderBy('lastPosted', 'desc')
.where(myUid exists in their followers subcollection)

I'm not sure where to go from here, or whether firestore even allows this.

AJ Riley
  • 85
  • 6

1 Answers1

1

See:

Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

zkohi
  • 2,486
  • 1
  • 10
  • 20
  • Firebase seriously doesn’t have an equivalent to `IN` in SQL? – Nate May 10 '19 at 13:28
  • See https://firebase.google.com/docs/firestore/query-data/queries . The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, >=, or array_contains. And Collection group queries. – zkohi May 10 '19 at 13:33
  • So that’s a no. Odd. – Nate May 10 '19 at 17:39