skip to Main Content

The Product A,B’s doc is in The collection[Product].
Also each product has their review collection. and User’s review is
updated in form of doc in that review collection

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


  1. Getting all collections first document(Default reverse order)

    Get the recent by time comparison

    Login or Signup to reply.
  2. 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.

    Something like this

    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.

    db.collection("reviews").
      .where("ProductId", "==", "1")
      .orderBy("CreateDate", descending: true)
      .limit(3);
    
    Login or Signup to reply.
  3. 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.

    exports.manageReviews = functions.firestore.document('/Products/{productId}/Reviews/{userId}').onWrite(async (change, context) => {
    // This cloud function listens to any review write or change by any user
    
    const after = change.after.data(); // The document after the change
    const userId = context.params.userId;
    const productId = context.params.productId;
    
    
    // Use this to make sure you have only the final review for each product
    await admin.firestore().collection("AllReviews").doc(productId).set(after)
    
    
    
     // Alternatively use this to consolidate all reviews of all products in a single table
    await admin.firestore().collection("AllReviews").add(after);
    
      //After that just query the collection AllReviews according to timestamp.
    });
    

    It might require some tweaking to suit your needs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search