2

i want to create a ranked list of dishes in my neighborhood. so far i can rank the dishes by votes.

// dish document structure
{
  dish_id: SOME_ID,
  categories: ["Soup", "Noodles"],
  votes: [SOME_USER_ID_1, SOME_USER_ID_2, ...]
}

The query

  const dishRef = collection(db, 'dishes');
  const q = query(
    dishRef,
    where('categories', 'array-contains', 'soup'),
    orderBy('votes', 'desc')
  );

now i want to rank them by votes which only include the ones of my friends in order to filter out irrelevant votes. let's say i want do query all soup dishes ordered by votes, where the votes counter only counts the votes of my friends. how do i achieve this in a NoSQL database/ firebase? how do i structure my data?

i have a collection of users and a collection of dishes so far. which kind of additional collections are required?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Thanh Le
  • 21
  • 1

1 Answers1

0

Firestore queries can only order/filter on fields in the documents that they return. So if you want to order/filter on the count of votes by your friends, you'll need to store exactly that value in the documents: friendUpvoteCount.

If you need to do this for all users, you'll quickly run into the maximum document size of 1MB. I can't immediately think of a different data structure for the use-case, so would probably consider either using another database that support this level of querying for it, or to see if you can tweak the use-case to fit with what Firebase supports.

For example, you could load the top upvoted restaurants overall, and then highlight which if this were also upvoted by friends of the user (since you've loaded the documents already).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807