简体   繁体   English

Flutter Firebase 子集合嵌套值查询

[英]Flutter Firebase Query on nested value in subcollection

I am looking for a way to query a stream based on information in a subcollection.我正在寻找一种基于子集合中的信息查询 stream 的方法。

Here is an example:这是一个例子:

I have a Subcollection of Tickets.我有一个门票子集。 Each Ticket has a Subcollection of Companies involved in this Ticket.每张工单都有一个涉及该工单的公司的子集。 Each Company has a CEO as a Map. This Map has a field "id".每个公司都有一个 Map 的 CEO。这个 Map 有一个字段“id”。

I now want to query the following thing: Get me a stream of all tickets where a company in this Ticket has a CEO with id "X".我现在想查询以下内容:给我一张 stream 的所有票据,其中一家公司的首席执行官 ID 为“X”。

I tried the following: refToTickets.where('companies.ceo.id', isEqualTo: CEOId).snapshots();我尝试了以下操作:refToTickets.where('companies.ceo.id', isEqualTo: CEOId).snapshots();

This doesn't work unfortunately.不幸的是,这不起作用。

Firestore queries can only filter on values that are present in the documents that they return. Firestore 查询只能过滤它们返回的文档中存在的值。 So you can't filter tickets on values from the companies in the subcollection.因此,您无法根据子集合中公司的值过滤票证。

As usual when dealing with NoSQL databases, you'll want to modify/expand your data model to allow the use case.与往常一样,在处理 NoSQL 数据库时,您需要修改/扩展数据 model 以允许用例。 Here for example, you could store the company CEOs in a single array field for each ticket and query on that.例如,您可以将公司 CEO 存储在每个票据的单个数组字段中并对其进行查询。 So each time you write/delete a document, you also update the companyCEOs field in the corresponding ticket.因此,每次您写入/删除文档时,您也会更新相应工单中的companyCEOs字段。

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

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