简体   繁体   English

如何查询到 Firestore 子文档

[英]How to query to Firestore sub document

I'm working on an app and I would like to query Firestore sub document.我正在开发一个应用程序,我想查询 Firestore 子文档。 Let me explain further.让我进一步解释。

  1. I have a collection of documents where cars are stored, each document has a particular car with description.我有一个存储汽车的文档集合,每个文档都有一个带有描述的特定汽车。
  2. In each of those documents above, I av a sub collection called user_data which have it's own document where a particular userid of the user who add the car to his wishlist is stored.在上面的每个文档中,我都有一个名为user_data的子集合,它有自己的文档,其中存储了将汽车添加到他的心愿单的用户的特定用户 ID。

Now I want to get the document of cars if a userid is present in its sub collection.现在,如果用户 ID 存在于其子集合中,我想获取汽车的文档。 In short, I want to get the wishlist of a particular user.简而言之,我想获取特定用户的愿望清单。 I'm using streambuilder with listviewbuilder but the problem is how do I perform this query?我正在将 streambuilder 与 listviewbuilder 一起使用,但问题是如何执行此查询?

Or is there any simpler way of doing this?或者有没有更简单的方法来做到这一点?

Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. Firestore 中的查询是浅层的,这意味着它们只能从运行查询的集合中获取项目。 There is no way to get documents from a top-level collection and other collections or subcollections in a single query.无法在单个查询中从顶级集合和其他集合或子集合中获取文档。 Firestore doesn't support queries across different collections in one step. Firestore 不支持一步跨不同集合的查询。 So you cannot get items from a collection based on the items that exist within a subcollection.因此,您无法根据子集合中存在的项目从集合中获取项目。 A single query may only use properties of documents in a single collection.单个查询只能使用单个集合中的文档属性。

In short, I want to get the wishlist of a particular user.简而言之,我想获取特定用户的愿望清单。

So the most simple solution I can think of, would be to add under each user object an array of favorite cars.所以我能想到的最简单的解决方案是在每个用户对象下添加一组最喜欢的汽车。 Your new database structure should look similar to this:您的新数据库结构应该类似于:

Firestore-root
    |
    --- users
          |
          --- uid
               |
               --- favoriteCars : ["carId", "carId"]

In this way you can query your database to get only the cars a user has marked them as favorite.通过这种方式,您可以查询数据库以仅获取用户将其标记为收藏的汽车。 You can also store instead of those ids in an array, the actual car object.您还可以将这些 id 存储在数组中,而不是存储实际的汽车对象。 Please see here more details about pros and cons.在此处查看有关优缺点的更多详细信息。

Currently Firebase Cloud Firestore doesn't support querying with sub collection.目前 Firebase Cloud Firestore 不支持使用子集合进行查询。 You may have to structure your database in way that's possible to query.您可能必须以可查询的方式构建数据库。

  • Store userid in a array in the car document.将用户 ID 存储在汽车文档中的数组中。
  • Use a separate collection to keep the connection between user and cars.使用单独的集合来保持用户和汽车之间的联系。

You can checkout this video from Firebase.您可以从 Firebase 查看此视频。

Maps, Arrays and Subcollections, Oh My!映射、数组和子集合,天哪! | | Get to Know Cloud Firestore了解 Cloud Firestore

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM