Firestore:按对象属性查询文档 [英] Firestore: Query documents by property of object

查看:71
本文介绍了Firestore:按对象属性查询文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个联系人集合,其结构如下:

I've a collection with contacts with a structure like:

name: 'XPTO Company',
emails: { 
    susan@xpto.com: { name: 'Susan', text: 'manager' },
    fred@xpto.com: { name: 'Fred', text: 'marketing' }
}

如何使用电子邮件"susan@xpto.com"检索文档

How do I retrieve documents with email 'susan@xpto.com'

类似的东西:

firebase.firestore().collection('contacts')
      .where(new firebase.firestore.FieldPath('emails', email), '==', true).get()
      .then(snap => {
      })

推荐答案

您可以将保存电子邮件地址的属性id添加到电子邮件对象.并替换为."在电子邮件对象键中带有-"的字符,例如susan@xpto-com,以便您的数据结构如下所示:

You may add a property id that holds the email address to your email object. and replace '.' in email object key with '-', e.g. susan@xpto-com, so that your data structure looks like this:

name: 'XPTO Company',
emails: { 
    susan@xpto-com: { id: 'susan@xpto.com', name: 'Susan', text: 'manager' },
    fred@xpto-com: { id: 'fred@xpto.com', name: 'Fred', text: 'marketing' }

然后,您可以通过以下方式使用电子邮件"susan@xpto.com"来检索文档:

Then you can retrieve documents with email 'susan@xpto.com' in this way:

firebase.firestore().collection('contacts')
      .where('emails.susan@xpto-com.id', '==', 'susan@xpto.com').get()
      .then(snap => {
      })

这篇关于Firestore:按对象属性查询文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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