Thursday, 2 December 2021

Using Where and Order by different fields in Firestore query

I have a Firestore collection channels, I'd like to get the list of channels based on an array of Ids and order it by the createdAt field, this is my function :

  const getChannels = () => {
    const q = query(
      collection(db, "channels"),
      where(documentId(), "in", [
        "F0mnR5rNdhwSLPZ57pTP",
        "G8p6TWSopLN4dNHJLH8d",
        "wMWMlJwa3m3lYINNjCLT",
      ]),
      orderBy("createdAt")
    );
    const unsubscribe = onSnapshot(q, (snapshot) => {
      snapshot.docs.map((doc) => {
        console.log(doc.data());
      });
    });
    return unsubscribe;
  };

I'm getting this error FirebaseError: inequality filter property and first sort order must be the same: __name__ and createdAt.

It only works if I orderBy documentId().

I'm aware there is a limitation in the docs about this, but I'm wondering if there is a workaround for this type of situation.

Also the answer for this question isn't working anymore I guess.



from Using Where and Order by different fields in Firestore query

No comments:

Post a Comment