1

My Firebase data structure is made as shown here:

data tree

I want to iterate and search for a specific user email address.

I've tried using dataSnapshot but after hours of work I ended up with nothing.

Would appreciate any help or examples.

Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mitch Tal
  • 49
  • 1
  • 8

1 Answers1

0

Your current structure allows you to easily find the users for a specific business. It does not however allow you to easily find a business for a specific user, or find a specific user across all businesses.

To allow the latter you'll want to store a flat list of users, so that you can search across that. A top-level node like this could work for this use case:

Users: {
  "$uid": {
    "email": "test@g.com",
    "business": "test (1593695305)",
    ...
  }
}

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you! So in other words I should change my data structure to a user-based instead of a business-based? What if there was another sub-list for a business along with "userList"? for example userList and orderList? – Mitch Tal Jul 02 '20 at 22:18
  • Not necessarily change it, as adding an additional structure to allow your additional use-case. This is quite common when working with NoSQL databases. For an intro on that, see [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) – Frank van Puffelen Jul 02 '20 at 22:23