Reputation: 11
I am building a simple player system for sports matches as simple as Player 1 vs Player 2 using Firestore. I would like to sort my database by timestamp, my matches collection looks like this:
{
Players:["eAQQSDEwqewqeqweq","WeqweqweqweW"],
Score:[3,2],
date:{Timestamp Obj}
}
Each player has a specific ID, that I use the result and date for every match. I do a query to find player specific matches by checking if in the array "Players" the specific id of the player exsists.
const response=db.collection('matches')
.where('Players', 'array-contains',id)
.orderBy('date','asc');
It returns empty, maybe because of the limited functionality of Firestore as I try to filter by one field then sort by another one. If I remove the .orderBy('date','asc'); line, it works.
So I want to pick the matches where a specific player takes part in and then sort them by date. What would be a possible way of solving the issue?
Upvotes: 0
Views: 102
Reputation: 11
I found ways to solve my issues, I will write a response to all noobies like me that did not understand what was going on.
Firebase lets you index by one field or multiple fields. When you use one filed query you don't need any additional set-up. When you need to do complex queries with multiple fields to query you need complex indexing, you need to use the firebase console platform, which is super easy, go to the project -> Firestore Database -> indexes. The indexing should be a complete match with your query. I made wrong indexing in the firebase console by selecting:
("date","asc"),("Players","asc")
I had to do:
("Players","array"),("date","asc")
So be careful about the right indexing.
Upvotes: 1