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

查看:24
本文介绍了是否可以在 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 提供一个 owner 属性.然后,您可以使用简单的 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天全站免登陆