Amer Alahmar
Amer Alahmar

Reputation: 85

Firestore How to orderBy a field in another document that's referenced in my document?

In Android (Java), I am using Firebase Firestore and trying to build a simple query to get data from a collection (users) and order it by a field that's in another collection (posts). In the (users) collection I have a field contains the path to a given document in (posts) collection.

users{
  userID1{
    name:"name"
    lastPost:posts/postID1
   }
}

posts{
  postID1{
    text:"text"
    postedAt:"timeStamp here" <-(The field I want to orderBy)
  }
}

This is the query:

Query query = FirebaseFirestore.getInstance()
            .collection("users")
            .orderBy(/*lastPost/postedAt**/);

Upvotes: 2

Views: 509

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

How to orderBy a filed in another document that's referenced in my document?

You cannot order documents that exist within a collection based on a property that exist in another collection even if you are holding a reference to that property.

So unfortunately, there's no way to extend a query across multiple subcollections. A query must be contained within a single collection or subcollection.

Upvotes: 2

Related Questions