如何查询Firestore子文档 [英] How to query to Firestore sub document

查看:91
本文介绍了如何查询Firestore子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我想查询Firestore子文档。让我进一步解释。

I'm working on an app and I would like to query Firestore sub document. Let me explain further.


  1. 我有一些存放汽车的文件,每个文件都有一辆带有描述的特定汽车。

  2. 在上述每个文档中,我都有一个名为 user_data 的子集合,该子集合具有其自己的文档,其中添加了

  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.

现在,如果子子集中存在用户ID,我想获取汽车文档。简而言之,我想获取特定用户的愿望清单。
我正在将streambuilder与listviewbuilder一起使用,但问题是如何执行此查询?

Now I want to get the document of cars if a userid is present in its sub collection. 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?

或者有没有更简单的方法?

Or is there any simpler way of doing this?

推荐答案

Firestore中的查询很浅,这意味着它们仅从运行查询的集合中获取项目。无法在单个查询中从顶级集合以及其他集合或子集合中获取文档。 Firestore一站式不支持跨不同馆藏的查询。因此,您无法基于子集合中存在的项目从集合中获取项目。单个查询只能使用单个集合中文档的属性。

Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. 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. 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"]

您可以查询数据库以仅获取用户将其标记为收藏的汽车。您还可以将实际的汽车对象而不是这些ID存储在数组中。请参阅 这里 有关优缺点的更多详细信息。

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. Please see here more details about pros and cons.

这篇关于如何查询Firestore子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆