0

This is my Firebase database structure:

firebase data structure image

Simply it looks like that. There are countries and users. If a user has visited a country it was stored in this user "VisitedCountries" child. for example user Angeline visited England and Turkey, may be she should want to see France, Japan, USA. So I want show a list which is query only 2 countries which was never visited by user before.

If it possibble I want to make it by server side by Firebase rules. How can I achieve that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mehmet
  • 1,558
  • 5
  • 30
  • 41
  • I don't really understand what you want exactly and what is has to do with the security rules. – André Kool Jul 18 '18 at 09:02
  • Well at this page it gives example for query based rules for firebase database I want solve this problem with query based rules if it posibble, https://firebase.googleblog.com/2018/01/introducing-query-based-security-rules.html – mehmet Jul 18 '18 at 09:09
  • I suggest you first get the query you want working before you write up security rules. – André Kool Jul 18 '18 at 09:20

1 Answers1

1

To solve this, first of all you need to query your database to get all the countries in your countries node. Second, add all those countries to a list. Once you have the list of countries, query the database to get all the countries Angeline visited, which are England and Turkey. So what you need to do, is to check if these countries already exist in countries list that you get before. If it doesn't exist, display two of them to Angeline. That's it!

For security rules I recommend you dig into the official documentation.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is there everything alright, can I help you with other informations? – Alex Mamo Jul 19 '18 at 08:12
  • "get all the countries" it looks expensive. because there will be lots of country in the future. are there any different aprocah without downloading all data? – mehmet Jul 19 '18 at 10:56
  • Is not expensive at all. Beside that, your database si well structured but you need to know that [You can loop in Firebase through billions of records](https://stackoverflow.com/questions/46329375/can-firebase-realtime-database-effectively-loop-through-billions-of-posts-and-re) without any problem. So looping through only **[195 countries](http://www.worldometers.info/geography/how-many-countries-are-there-in-the-world/)** will never be a problem, right? – Alex Mamo Jul 19 '18 at 11:02
  • well for performans you are right. in my app my database size 1MB but usage 30GB so I am paying money. – mehmet Jul 19 '18 at 12:17
  • But you are not paying for the size, you are paying because it's very popular which is a good thing. But I don't understand your fear. The list of contries is a fixed list is not something that is changing. So you get that list only once. If you think that Firebase Real-time database does not fit your needed then move to [Firestore](https://firebase.google.com/docs/firestore/pricing). There is a different pricing plan, right? – Alex Mamo Jul 19 '18 at 12:40