简体   繁体   中英

Firestore: Query documents by property of object

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' }
}

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

Something like:

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

You may add a property id that holds the email address to your email object. and replace '.' in email object key with '-', eg 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' }

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 => {
      })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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