简体   繁体   English

如何阅读 firebase 收藏的最近文档?

[英]how to read recent document in collection with firebase?

The Product A,B's doc is in The collection[Product].产品 A、B 的文档在集合 [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.但是包含文档的 collections 都是不同的。 If reading two collections at once is not possible, What should I do?如果一次读取两个 collections 是不可能的,我该怎么办? How do you usually make this widget?你通常如何制作这个小部件? Getting all collections, merging them and sorting them by timestamp cost too much to read, I think.我认为,获取所有 collections、合并它们并按时间戳对它们进行排序成本太高,难以阅读。

(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').首先在 doc('product A') 中设置用户 A 的文档的路径。 And later, when userB writes a review, the reference field value is updated to userB's path.稍后,当用户 B 撰写评论时,引用字段值将更新为用户 B 的路径。

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我正在使用 flutter

Getting all collections first document(Default reverse order)获取所有 collections 第一个文档(默认倒序)

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.然后你只需要运行一些命令,比如评论集合中的以下命令,以获得产品 1 的最新评论。

db.collection("reviews").
  .where("ProductId", "==", "1")
  .orderBy("CreateDate", descending: true)
  .limit(3);

Looks like a cloud function would solve your problem.看起来云 function 可以解决您的问题。 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.它可能需要一些调整以满足您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM