简体   繁体   English

firebase 查询 - 查找 map 值在数组中的文档

[英]firebase query - find doc where map value is in an array

I'm trying to find the id of a doc where a map value in an array of maps equals "x".我正在尝试查找文档的 ID,其中地图数组中的 map 值等于“x”。

in the following case, I'm trying to find which rep owns the cause with code "hog"在以下情况下,我试图找出代码为“hog”的原因归因于哪个代表

I'll likely be going down the denormalizing route, but is this possible?我可能会走非规范化路线,但这可能吗?

在此处输入图像描述

Firestore has an array-contains operator that you can use to query whether a certain item exists in an array field , but that operator only works if you specify the exact, complete value of the field. Firestore 有一个array-contains运算符,您可以使用它来查询某个项目是否存在于数组字段中,但该运算符仅在您指定该字段的准确、完整值时才有效。 It can't test for a partial match.它无法测试部分匹配。

The common approach to your use-case is to add an additional array field with just the values you want to query on, ie您的用例的常用方法是添加一个额外的数组字段,其中仅包含您要查询的值,即

cause-codes: ["hog"]

Once you modified your documents with this additional field, you can then use a query like:使用此附加字段修改文档后,您可以使用如下查询:

repsRef.where('cause-codes:', 'array-contains', 'hof')

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

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