简体   繁体   English

如何在Firestore集合中查询其值包含在列表中的文档

[英]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集合: UsersPosts 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 have owner values contained in the user's friends 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.

相关问题 查询列表中包含 Firestore 字段的位置 - Query where a Firestore Field is contained in a List Firestore查询列表中存在uid的所有文档 - Firestore query for all documents whose uid exists in a list 如何根据每个文档中包含的一个值有效地对 Firestore 文档列表进行排序? - How to efficiently sort a list of firestore documents based on one value contained in each document? 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? 如何在firestore中查询带有时间戳字段的文档? - How to query firestore for documents with a timestamp field? 是否可以在 Cloud Firestore 上查询 Field 并获取主集合字段并获取带有子集合文档的子集合? - Is it possible to query Field and get main collection field and get sub collection with sub collection documents on cloud firestore? 从 firestore 中的文档查询集合创建列表 - create the list from the query collection of documents from firestore 如果我使用Query在“Android”中拥有用户ID arraylist,如何从“Firestore”集合中获取特定的文档列表 - How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query 如何在一个 Firestore 集合中查询另一个 Firestore 集合文档中包含的值 - How to Query One Firestore Collection for Values Contained within another Firestore Collection Document 在 Firestore 中,如何更改某个字段具有某个值的集合的所有文档,该字段具有某个值? - In Firestore, how do I change for all documents of a collection of which a certain field has a certain value, that field in a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM