[英]How to query Firestore collection for documents with field whose value is contained in a list
I have two Firestore collections, Users
and Posts
. 我有两个Firestore集合: Users
和Posts
。 Below are simplified examples of what the typical document in each contains. 以下是每个文档中包含的典型文档的简化示例。
*Note that the document IDs in the friends
subcollection are equal to the document ID of the corresponding user documents. *请注意, friends
子集合中的文档ID等于相应用户文档的文档ID。 Optionally, I could also add a uid
field to the friends
documents and/or the Users
documents. (可选)我也可以将uid
字段添加到friends
文档和/或Users
文档中。 Also, there is a reason not relevant to this question that we have friends
as a subcollection to each user, but if need-be we change it into a unified root-level Friends
collection. 同样,有一个与这个问题无关的原因是我们将friends
作为每个用户的子集合,但是如果需要,我们可以将其更改为统一的根级别的Friends
集合。
This setup makes it very easy to query for posts, sorted chronologically, by any given user by simply looking for Posts
documents whose owner
field is equal to the document reference of that user. 此设置使查找给定用户按时间顺序排序的帖子变得非常容易,只需查找其owner
字段等于该用户的文档引用的Posts
文档即可。
I achieve this in iOS/Swift with the following, though we are building this app for iOS, Android, and web. 尽管我们正在为iOS,Android 和 Web构建此应用,但我可以通过以下方式在iOS / Swift中实现此目的。
guard let uid = Auth.auth().currentUser?.uid else {
print("No UID")
return
}
let firestoreUserRef = firestore.collection("Users").document(uid)
firestorePostsQuery = firestore.collection("Posts").whereField("owner", isEqualTo: firestoreUserRef).order(by: "timestamp", descending: true).limit(to: 25)
My question is how to query
Posts
documents that haveowner
values contained in the user'sfriends
subcollection, sorted chronologically. 我的问题是如何查询Posts
有文件owner
包含在用户的价值观friends
子集合,按时间顺序进行排序。 In other words, how to get the posts belonging to the user's friends, sorted chronologically. 换句话说,如何获取按用户时间排序的属于用户朋友的帖子。
For a real-world example, consider Twitter, where a given user's feed is populated by all tweets that have an owner property whose value is contained in the user's following list, sorted chronologically. 对于一个真实的例子,考虑Twitter,其中给定用户的提要由所有具有owner属性的推文填充,这些属性的值包含在用户的后续列表中,并按时间顺序排序。
Now, I know from the documentation that Firestore does not support logical OR queries, so I can't just chain all of the friends together. 现在,我从文档中知道Firestore不支持逻辑OR查询,因此我不能仅将所有朋友链接在一起。 Even if I could, that doesn't really seem like an optimal approach for anyone with more than a small handful of friends. 即使我可以,对于只有少数几个朋友的人来说,这似乎也不是一种最佳方法。
The only option I can think of is to create a separate query for each friend. 我能想到的唯一选择是为每个朋友创建一个单独的查询。 There are several problems with this, however. 但是,这有几个问题。 The first being the challenges presenting (in a smooth manner) the results from many asynchronous fetches. 首先是挑战(以一种流畅的方式)呈现许多异步获取的结果。 The second being that I can't merge the data into chronological order without re-sorting the set manually on the client every time one of the query snapshots is updated (ie, real-time update). 第二个原因是,每次更新一个查询快照(即实时更新)时,如果不在客户端上手动对集合进行重新排序,就无法按时间顺序将数据合并。
Is it possible to build the query I am describing, or am I going to have to go this less-than optimal approach? 是否可以构建我正在描述的查询,还是我不得不采用这种非最佳方法? This seems like a fairly common query use-case, so I'll be surprised if there is not a way to do this. 这似乎是一个相当常见的查询用例,所以如果没有一种方法可以使我感到惊讶。
The sort chronologically is easy provided you are using a Unix timestamp, eg 1547608677790
using the .orderBy
method. 如果您使用的是Unix时间戳, 1547608677790
时间顺序进行排序很容易,例如,使用.orderBy
方法的1547608677790
。 However, that leaves you with a potential mountain of queries to iterate through (one per friend). 但是,这使您有大量潜在的查询要迭代(每个朋友一个)。
So, I think you want to re-think the data store schema. 因此,我认为您想重新考虑数据存储架构。
Take advantage of Cloud Functions for Firebase Triggers . 利用Cloud功能进行Firebase触发器 。 When a new post is written, have a cloud function calculate who all should see it. 撰写新帖子时,请使用云功能来计算应该看到谁。 Each user could have an array-type property containing all unread-posts
, read-posts
, etc. 每个用户都可以具有包含所有unread-posts
,已read-posts
等的array-type属性。
Something like that would be fast and least taxing. 这样的事情将是最快且最省事的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.