0

So I found this answer on how to create a unique field in Firebase: Firebase android : make username unique

But my question is, if I have multiple unique fields (in different collections) does that mean I have to create multiple usernames collections that will hold all my unique fields.

Here is an example. Say I have two collections users and groups. In my users, I have an email field that must be unique. In my groups I have an address field that must be unique. So does that mean (according to the above answer) I need to have these collections in my root:

  • users
  • uniqueUserEmails
  • groups
  • uniqueGroupAddresses

This seems horrible? Is this a big downside to nosql vs sql? In sql it would be so easy to say UNIQUE in the field(column) creation

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Paul Kruger
  • 2,094
  • 7
  • 22
  • 49

1 Answers1

1

If you need some value (or combination of values) to be unique, you need to create a node that contains that value (or combination) as its key. If you need to guarantee that multiple values (or combinations) are unique, you'll need multiple of such nodes.

When you have a database that does support uniqueness constructs, it is pretty much doing the same behind the scenes. The only difference is that the database then does it automatically, where here you have to do it yourself.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My users was this `[GUID123: {name: 'Frank', email: 'x@g.com'}, GUID456: {name 'John', email: 'x2@g.com'}]` should I then rather create this: `[x@g.com: {name: 'Frank'}, x2@g.com: {name 'John'}]`? – Paul Kruger Jun 23 '19 at 14:07
  • Ignore the `[]` sorry, should be `{}` – Paul Kruger Jun 23 '19 at 14:14
  • `you need to create a node` What do you mean with this? Do you mean the same as `you need to create a collection`? – Paul Kruger Jun 23 '19 at 14:43
  • The question you linked to is about the Firebase Realtime Database, where the data model is based on nodes and value. In Firestore that would indeed be a collection. – Frank van Puffelen Jun 23 '19 at 14:48