Firestore:如何从数组的映射中查询数据 [英] Firestore: How to query data from a map of an array

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

问题描述

这是测试数据库的架构:

Here is the schema for test db:

我想编写一个查询,该查询可以获取任何数组索引中联系人的 id=1 的所有文档.

I want to write a query that can get all the documents where contacts have id=1 in any of the array index.

我已经检查了 firestore 的 array_contains 运算符,但问题是我的数组有映射,然后有字段 ID.

I have checked array_contains operator for firestore but the thing is my array have map which then have field id.

谢谢

推荐答案

您目前无法构建在数组中查找对象字段的查询.在数组中你只能找到数组元素的全部内容,而不是元素的一部分.

You currently can't build a query that looks for an object field within an array. The only things you can find in an array are the entire contents of an array element, not a part of an element.

对于 NoSQL 类型的数据库,通常的做法是以适合您查询的方式构建数据.因此,在您的情况下,您将不得不对数据进行结构化以让您找到数组项包含特定字符串的文档.因此,您可以创建另一个仅包含要查询的字符串的数组.您必须确保使这些数组保持最新.

With NoSQL type databases, the usual practice is to structure you data in a way that suits your queries. So, in your case, you're going to have to structure you data to let you find documents where an array item contains a certain string. So, you could create another array that contains just the strings you want to query for. You will have make sure to keep these arrays up to date.

您也可以在此处重新考虑使用数组.数组适用于位置数据,但除非这些联系人需要按特定顺序存储,否则您可能希望存储一个对象,其键是 id,其字段数据还包含 id 和名称.

You could also reconsider the use of an array here. Arrays are good for positional data, but unless these contacts need to be stored in a certain order, you might want to instead store an object whose keys are the id, and whose field data also contains the id and name.

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

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