Firestore: Query documents by property of object

后端 未结 1 1380
南方客
南方客 2021-01-14 07:34

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

name: \'XPTO Company\',
emails: { 
    susan@xpto.com: { name: \'Susan\', text: \'manager\' },
    fr         


        
相关标签:
1条回答
  • 2021-01-14 07:58

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

    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 => {
          })
    
    0 讨论(0)
提交回复
热议问题