3

Here's my data structure:

root
---+ mydata
    |--- -KqI0uf--1-bD3oLlx2h:"Joe"
    |--- -Kdk0bf--1-bx0oLlb9a:"Mike"
    |--- -Kox8gf--1-b36onlT3h:"Eve"

How do I write a rule to only allow unique values in my Firebase Realtime Database?

For example, I don't want this to happen:

root
---+ mydata
    |--- -KqI0uf--1-bD3oLlx2h:"Boss"
    |--- -Kdk0bf-3-bx0oLlb9a:"Bob"
    |--- -Kox8gf1-b36onlT3h:"Dot"
    |--- -Kbx8gff-1-b92ksvT7:"Bob" // this should not exist
Jonathan Doe
  • 101
  • 5
  • 20
  • I'll accept the working answer. – Jonathan Doe Jul 30 '17 at 11:41
  • 2
    The Firebase Database has no efficient mechanism to ensure values within a collection are unique. On the other hand: the keys in a collection are unique by definition. So the solution typically is to store the user names as the keys in the collection. – Frank van Puffelen Jul 30 '17 at 13:33
  • @FrankvanPuffelen What I'm storing actually are the UIDs of the users. Do you have an efficient way to keep them unique in my database? Thanks. – Jonathan Doe Jul 30 '17 at 13:36
  • Same as any other value you want to keep unique: store them as keys in a collection. The push IDs are likely meaningless in your collection anyway. Also see my explanation on arrays vs sets, here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jul 30 '17 at 13:50
  • 1
    I suppose you are using "push()" to send data to firebase database. Instead you can just create your own unique key and using "child()" function for firebase send data as model. – Jay Apr 22 '18 at 04:16

0 Answers0