我当前的结构如下所示
收藏帖子
- PostID
- titel
- content
- titelimgurl
- tags
- timestamp
现在在这里获取数据很容易,我想要实现的是将内容与其余内容分开,这样我就不必在我的首页上获取不需要的数据。
所以我打算采用这种结构:
收藏帖子
- 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);
});
这看起来过于复杂,特别是当将此数据分配给我的商店中的实际帖子集合时,任何关于如何提高效率的想法都值得赞赏。
最佳答案
如果您只想查询数据库一次,则不应通过将该内容
创建为顶级集合来对数据库进行非规范化。您可以将帖子内容保留为 PostID
文档中的属性。与 Firebase 实时数据库不同,在 Firebase 实时数据库中显示 post
对象列表,您将下载整个 posts
节点,在 Cloud Firestore 中这不再是问题。因此,您可以随意将内容保留为属性,以便进行单个查询。
关于javascript - 从两个相关集合查询数据的最简洁的解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003227/