I’m making a product display app.
Users can write reviews for each product.
I’m trying to create a ‘Recently Written Review’ widget "regardless of the product".
I know I need to get documents according to timestamp.
But the collections containing the doc are all different.
If reading two collections at once is not possible, What should I do?
How do you usually make this widget?
Getting all collections, merging them and sorting them by timestamp
cost too much to read, I think.
(Currently, I structured data as the picture.)
What I’m thinking of is,
first set the path to userA’s doc in doc(‘product A’).
And later, when userB writes a review, the reference field value is updated to userB’s path.
Is there a more efficient way than this?
please give me a good answer
thank you for reading and answering this question!
Im using flutter
3
Answers
Getting all collections first document(Default reverse order)
Get the recent by time comparison
It looks like you are thinking about this being complex thats why you are stuck in this kind of situation.
You need to simplify the collection of review. You are creating different collection for each of the product but you just need one table so your queries will be same and easy.
Here you can use the same review collection/table to save review of all products by all users. Using this structure you can easily get reviews by product, reviews by users and users/products by reviews.
Then you just have to run some command like following on reviews collection to get the latest review of product 1.
Looks like a cloud function would solve your problem. The following code consolidates all reviews into a single collection so you could query that for your widget.
It might require some tweaking to suit your needs.