简体   繁体   English

如何从 Firestore 获取数组值?

[英]How to get an array value from Firestore?

I have the data structure illustrated below stored in Cloud Firestore.我将如下所示的数据结构存储在 Cloud Firestore 中。 I have saved company record in Firestore database and i want to check my domain is exist in company's domain array or not and if yes then i want all company data in response but i have no idea how can i achieve this with Firestore.我已经在 Firestore 数据库中保存了公司记录,我想检查我的域是否存在于公司的域数组中,如果是,那么我想要所有公司数据作为响应,但我不知道如何使用 Firestore 实现这一点。

在此处输入图片说明

Here i have marked domains.我在这里标记了域。 let me explain more, For Example if my domain name is "abcinfo.in" then i want to check that is it already exist with any partner_company domains or not and here it is linked with this partner_company so it should return me all data of partner_company.让我解释一下,例如,如果我的域名是“abcinfo.in”,那么我想检查它是否已经存在于任何 partner_company 域中,这里它与这个 partner_company 相关联,所以它应该返回给我 partner_company 的所有数据. That's it.就是这样。

I have tried with below code but it helps when there are single value but here i need to check from array我已经尝试过下面的代码,但是当有单个值时它会有所帮助,但在这里我需要从数组中检查

 var domaindata = db.collection('partner_company').where('company_domain', '==', _user.domain);
  domaindata.get().then(function (querySnapshot) {
    querySnapshot.forEach(function (doc) {
      console.log(doc.id, ' => ', doc.data());
    });
  });

So please suggest me any better way to achieve data that i want.所以请建议我任何更好的方法来获得我想要的数据。 Thank you.谢谢你。

Firebase comes with a neat feature for array filtering: array-contains and array-contains-any . Firebase 带有一个用于数组过滤的简洁功能: array-containsarray-contains-any They can be used as a query operator.它们可以用作查询运算符。

Use array-contains to filter arrays for values that have to match perfectly.使用 array-contains 过滤数组以查找必须完全匹配的值。 The following query would resolve your test document:以下查询将解析您的测试文档:

db.collection('partner_company').where('company_domain', 'array-contains', ['abcinfo.in', 'newtest.com'];

array-contains-any on the other side filters for at least one matching value. array-contains-any 在另一侧过滤至少一个匹配值。

db.collection('partner_company').where('company_domain', 'array-contains-any', ['definfo.in', 'newtest.com'];

Read more about array membership in firebase here此处阅读有关 firebase 中数组成员的更多信息

You can do the following:您可以执行以下操作:

var domaindata = db.collection('partner_company').where('company_domain', 'array-contains', _user.domain);

This will check if the array company_domain contains the domain passed.这将检查数组company_domain包含传递的域。

Check the following:检查以下内容:

https://firebase.google.com/docs/firestore/query-data/queries#array_membership https://firebase.google.com/docs/firestore/query-data/queries#array_membership

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

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