是否可以在Cloud Firestore中查询多个文档子集合? [英] Is it possible to query multiple document sub-collections in Cloud Firestore?

查看:75
本文介绍了是否可以在Cloud Firestore中查询多个文档子集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为使用Cloud Firestore的项目规划数据库的结构,而我目前正在尝试找出存储数据并将其与用户关联的最佳方法。详细信息如下:

I'm planning the structure of my DB for a project using Cloud Firestore, and I'm currently trying to figure out the best approach to storing data and associating it to users. The specifics are as follows:

我在数据库的根目录中有一个用户文档集合,我希望用户能够发布可供以下人员访问的帖子:查看显示该用户帖子的用户个人资料页面,以及查看所有用户显示帖子的页面。

I have a collection of user documents at the root of my DB, and I want users to be able to make posts that are accessible both by looking at a user profile page which displays posts by that user, and in aggregate by looking at a page displaying posts by all users.

将用户的帖子存储在子目录中收集用户文档将使其很容易获取其个人资料的个人帖子,但是是否有可能编写一个优雅的查询将每个用户的帖子子集合合并到一个结果集中?

Storing a user's posts in a sub-collection of the user document would make it very easy to fetch a user's posts for their profile, but is it possible to write an elegant query that merges every user's post sub-collections into one result set?

或者更好的方法是在我的被调用帖子的根目录中有另一个集合,并在每个帖子上都有一个包含用户的UID或用户文档的非规范化副本的字段?

Or would a better approach to be to have another collection in the root of my called posts, and have a field on each post containing the user's UID or a denormalized copy of the user document?

推荐答案

查询针对单个文档集合运行。 (当前)无法跨多个(子)集合进行查询。

A query runs against a single document collection. There is (currently) no way to query across multiple (sub)collections.

这意味着您可能希望将用户文档保留在顶层 documents 集合,其UID具有所有者属性。然后,您可以选择带有简单 documentsRef.where( owner, ==,uid)的用户帖子。

That means you'll probably want to keep the user documents in a top-level documents collection, with a owner property for their UIDs. Then you can select a user's post with a simple documentsRef.where("owner", "==", uid).

这篇关于是否可以在Cloud Firestore中查询多个文档子集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆