Firestore: How to query data from a map of an array

人走茶凉 提交于 2020-02-02 05:26:50

问题


Here is the schema for test db:

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

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

Thanks


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/54600915/firestore-how-to-query-data-from-a-map-of-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!