简体   繁体   English

如何在一次通话中获取 Firebase 中的子集合的子集合

[英]How can i get sub-collections of sub-collections in Firebase in one call

I have a firebase schema that look like this: posts > posts(documents) > comments > comments(collections) > replies > replies(documents)我有一个看起来像这样的 firebase 架构:帖子 > 帖子(文档) > 评论 > 评论(收藏) > 回复 > 回复(文档)

const allComments = await firebase
  .collection('posts')
  .doc(params.slug as string)
  .collection('comments')
  .orderBy("date", "desc")
  .get()

if i tried to get the reply like this:如果我试图得到这样的答复:

const allComments = await firebase
  .collection('posts')
  .doc(params.slug as string)
  .collection('comments')
  .doc("some_random_id")
  .collection('reply')
  .get()

i will get only the reply, which is not the desired output.我只会得到答复,这不是所需的 output。 i need to get each reply of each comment in one call.我需要在一个电话中获得每条评论的每条回复。 so the returned object would be every comment, and each comment contains an object of replies.所以返回的 object 将是每条评论,每条评论都包含一个 object 回复。

Firestore queries are "shallow", and only return documents immediately within the collection being queried. Firestore 查询是“浅层”的,仅在被查询的集合中立即返回文档。 They don't consider any documents from subcollections.他们不考虑子集合中的任何文档。 To get documents from subcollections, you would need to make another query for each subcollection.要从子集合中获取文档,您需要对每个子集合进行另一个查询。

It might be better to simply put all the comments and replies in a single collection, mark them each with a boolean indicating if it's a reply or not, and use that as a filter if needed on the query.最好将所有评论和回复简单地放在一个集合中,用 boolean 标记它们是否是回复,并在查询需要时将其用作过滤器。 The size of the collection will not have any impact on performance.集合的大小不会对性能产生任何影响。

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

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