本文介绍了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.

我已经检查了array_contains运算符是否为firestore,但问题是我的数组具有map,然后具有field 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:如何从数组的映射查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:50