Firestore:按对象属性查询文档

Inf*_*tus 5 firebase google-cloud-firestore

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

name: 'XPTO Company',
emails: { 
    susan@xpto.com: { name: 'Susan', text: 'manager' },
    fred@xpto.com: { name: 'Fred', text: 'marketing' }
}
Run Code Online (Sandbox Code Playgroud)

如何通过电子邮件“susan@xpto.com”检索文档

就像是:

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

Mus*_*hba 7

id您可以将保存电子邮件地址的属性添加到您的电子邮件对象。并替换 '.' 在电子邮件对象键中添加“-”,例如susan@xpto-com,这样您的数据结构如下所示:

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' }
Run Code Online (Sandbox Code Playgroud)

然后您可以通过电子邮件“susan@xpto.com”检索文档,方法如下:

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