简体   繁体   English

如何使用 javascript 从 firestore 查询数组?

[英]How to query the array from the firestore using javascript?

So, I have this firestore that looks like the picture below.所以,我有一个如下图所示的 Firestore。 How can I query the arrays that I highlighted only to javascript format?如何查询我仅突出显示为 javascript 格式的 arrays?

我想要的数组是突出显示的

How the Getting started with firestore does is by using [array-contains](https://firebase.google.com/docs/firestore/query-data/queries#:~:text=const%20q%20%3D%20query(citiesRef%2C%20where(%22regions,array_contains_filter.js) which looks like this as their example: Firestore 入门的方法是使用 [array-contains](https://firebase.google.com/docs/firestore/query-data/queries#:~:text=const%20q%20%3D%20query (citiesRef%2C%20where(%22regions,array_contains_filter.js) 看起来像他们的例子:

const q = query(citiesRef, where("regions", "array-contains", "west_coast"));

but what I see is that it only displays the word 'west_coast', what I want is that it will display any information in the field called 'amount' and 'title' as the picture above但我看到的是它只显示'west_coast'这个词,我想要的是它会在名为'amount'和'title'的字段中显示任何信息,如上图所示

I'm afraid you can't.恐怕你不能。 You need to retrieve the whole products array and filter locally.您需要检索整个products数组并在本地进行过滤。

Which leads to how your data is structured in Firestore.这会导致您的数据在 Firestore 中的结构。 If that's the query you need, your data must have probably been structured differently.如果那是您需要的查询,那么您的数据可能具有不同的结构。

For example:例如:

  • products could be a subcollection of ordered products可以是ordered的子集合
  • or, ordered documents may store the data you need to query in a separate, simplified array (like titles: [...], amounts: [...] , but that won't help for sorting or ranges in numbers anyway).或者, ordered文档可能会将您需要查询的数据存储在一个单独的简化数组中(如titles: [...], amounts: [...] ,但这对排序或数字范围没有帮助) .

Take a look at: https://firebase.google.com/docs/firestore/manage-data/structure-data for examples and use cases like this.看看: https://firebase.google.com/docs/firestore/manage-data/structure-data示例和用例。

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

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