代码之家  ›  专栏  ›  技术社区  ›  niclas_4

从两个相关集合查询数据的最干净的解决方案是什么?

  •  0
  • niclas_4  · 技术社区  · 6 年前

    我现在的结构是这样的

    收集站

    - PostID
           - titel
           - content
           - titelimgurl
           - tags
           - timestamp
    

    现在在这里获取数据很容易,我想要实现的是将内容从其他内容中分离出来,这样我就不必在frotnpage上获取不需要的数据。

    所以我打算采用这种结构:

    收集站

    - PostID
           - titel
           - titelimgurl
           - tags
           - timestamp
    

    收藏内容

    - PostID
           - content
    

    问题是我不确定如何在那里有效地检索数据,例如:

    var postRef = db.collection("posts").doc(this.docID);
    
    postRef.get().then(function(doc) {
        if (doc.exists) {
            var contentRef = db.collection("content").doc(doc.id)
            contentRef.get().then(function(doc) {
                ....
             }
        } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });
    

    这看起来过于复杂,特别是当把这些数据分配给我的商店中的一组帖子时,任何关于如何提高效率的想法都值得赞赏。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alex Mamo    6 年前

    如果只想查询一次数据库,则不应该通过创建 content 作为顶级收藏。你可以把文章的内容作为 PostID 文件。与firebase实时数据库不同,在其中显示 post 对象,则会下载整个 posts 节点,在云FireStore中,这不再是问题。因此,您可以随意将内容保留为属性,以便进行单个查询。