Angular 如何在firestore中获取具有以下数据结构的文档?
我被firestore中的数据结构获取所困扰。我只能满足1项要求。请帮我做这个 要求:Angular 如何在firestore中获取具有以下数据结构的文档?,angular,firebase,ionic-framework,collections,google-cloud-firestore,Angular,Firebase,Ionic Framework,Collections,Google Cloud Firestore,我被firestore中的数据结构获取所困扰。我只能满足1项要求。请帮我做这个 要求: 显示最新的k篇文章,而不考虑任何用户(整个数据库中最新的k篇文章) 显示与多个用户对应的帖子(与我跟踪的用户对应的帖子) 下面是我可以想到的两种数据结构,但仍然无法满足第二个要求 数据结构1: 要求1:显示最新的k篇文章,不考虑任何用户 constructor(private afs: AngularFirestore) {} // code to fetch latest k posts this.afs
constructor(private afs: AngularFirestore) {}
// code to fetch latest k posts
this.afs.collection("posts").ref
.orderBy("date", "desc")
.limit(k)
.get().then(snapshot => {
snapshot.forEach(postDoc => {
console.log(postDoc.data());
// rest of the code
});
});
Req2:显示多个用户对应的帖子
问题:在上面的代码中,如果我添加一个“where”方法,它只能过滤一个authord,不能过滤多个authord。
e、 g
数据结构2:
不确定,但我认为数据结构会比数据结构更有效,因为在查找与我跟踪的用户对应的帖子时,不需要搜索整个帖子集合,而只需查看您跟踪的用户的帖子集合。
问题:如何在上述数据结构中获取最新的k个帖子?您应该毫不犹豫地在数据库中维护这两个数据结构。在NoSQL数据库中,这是一种非常经典的方法,称为“非规范化”(与众所周知的SQLDBS规范化相反) 看看这篇文章,它解释了NoSQL数据库的“数据建模技术”,并说“非规范化可以定义为将相同的数据复制到多个文档或表中,以简化/优化查询处理,或将用户的数据适配到特定的数据模型中” 缺点是您必须保持两个结构的同步,但这可以通过Firestore中的批处理写入轻松实现,请参见此处的文档
constructor(private afs: AngularFirestore) {}
// code to fetch latest k posts
this.afs.collection("posts").ref
.orderBy("date", "desc")
.limit(k)
.get().then(snapshot => {
snapshot.forEach(postDoc => {
console.log(postDoc.data());
// rest of the code
});
});
this.afs.collection("posts").ref
.where("authorId" == "xyz...")
.orderBy("date", "desc")
.limit(k)
/users/{userId}/
-name
-etc...
/posts/{postId}/ (collection inside user document)
-message
-date