简体   繁体   English

我可以使用 Firestore 检索具有相同名称的不同集合中相同子集合的文档吗?

[英]Can I retrieve documents of same subcollection in different collections with the same name, using Firestore?

I'm working on an Android app and I use Firestore to store my data.我正在开发一个 Android 应用程序,我使用 Firestore 来存储我的数据。 I have a collection of Users , and every user has a subcollection called Posts .我有一个Users集合,每个用户都有一个名为Posts的子集合。 Is there any way to retrieve all posts from all users to show them on my apps feed?有什么方法可以检索所有用户的所有帖子以在我的应用程序提要中显示它们吗?

Is there any way to retrieve all posts from all users to show them on my apps feed?有什么方法可以检索所有用户的所有帖子以在我的应用程序提要中显示它们吗?

Sure, it is.当然是啦。 You need to use collection group queries :您需要使用集合组查询

Use a collection group query to retrieve documents from a collection group instead of from a single collection.使用集合组查询从集合组而不是从单个集合中检索文档。 In code should look like this:在代码中应该是这样的:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collectionGroup("Posts").get().addOnCompleteListener(/* ... */);

Using the above code, you'll be able to get all Post objects that exist within all subcollections named Posts .使用上面的代码,您将能够获取存在于所有名为Posts的子集合中的所有Post对象。

You will want to use a collection group query to get all the documents from all subcollections with the same name.您将希望使用集合组查询从所有子集合中获取同名的所有文档。 For example:例如:

db.collectionGroup("Posts").get()

暂无
暂无

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

相关问题 为什么使用 subCollection 同时创建多个文档显示为斜体? - why Creating multiple documents at same time with subCollection appear as Italic? 在 Google Cloud Firestore 的子集合中更新依赖于多个文档的字段时,如何避免竞争条件? - How can I avoid a race condition when updating a field that's dependent multiple documents in a subcollection in Google cloud firestore? 从子集合 Firestore 中获取文档 swiftUI - getting documents from a subcollection Firestore swiftUI 在 Firestore 中,如何防止“请确保您使用的 Firestore 类型来自同一个 NPM 包”错误 - In Firestore, how can I prevent "Please ensure that the Firestore types you are using are from the same NPM package" errors SwiftUI 和 Firestore - 启动时从不同 collections 读取多个文档的逻辑 - SwiftUI and Firestore - logic for reading multiple documents from different collections at startup Firestore 规则:使集合中的文档和所有子集合遵循相同的规则 - Firestore Rules: Making documents in a collection and all subcollections follow the same rule 如何定义只有登录用户才能读取和写入所有 collections 和子集合的安全规则? - How do I define the security rules where only logged in user can read and write all of the collections and subcollection? 我可以记录 Firestore 文档的更改吗? - Can i record changes on firestore documents? 如何在 Firestore 中检索引用的文档? - How to retrieve referenced documents in Firestore? 如何在 firestore 中获取子集合路径 - how do I get subcollection path in firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM