[英]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.