I just came from the relational database school and dealing with JSON databases is not an easy task for new comers. I have this structure to store users:
{
"users" : {
"0CcKvNkOm5fVqL" : {
"birthday" : 564688000,
"country" : "US",
"email" : "email@live.com",
"firstName" : "John",
"gender" : "male",
"isOnline" : true,
"lastLoginDate" : 1468166460486,
"lastName" : "Paul",
"learningLanguages" : [ {
"language" : "fr_FR",
"levelID" : 2
} ],
"profileImage" : "https://firebasestorage.googleapis.com/image.jpg",
"providerID" : "Firebase",
"registrationDate" : 1468168460486,
"speakingLanguages" : [ {
"language" : "es_ES",
"levelID" : 7
} ]
}
}
}
I offer a search screen in my app where users can search for other users and they can combine all these filter parameters:
Example:
Get 10
users starting from index 0
who are:
male
- and from
"US"
- and
speaks "da_DK"
withlevelID 2
or/and"fr_FR"
withany level
- and learns "de_DE"
with
level 1**and/or**
learns "ar_AR"with
level 4` - and with
age range between 18 and 24
- and order by
isOnline
andlast login date
.
This is an easy task with SQL, when supposing that there is a table called users_languages:
SELECT ...
FROM users AS u
JOIN users_languages AS l
ON u.id = l.id
WHERE u.gender = "male"
AND u.age BETWEEN 18 AND 24 // need claculation but let's keep it simple
AND u.country = "US"
AND ((l.language = "de_DE" AND l.mode = "learning" AND l.level = 1) OR (l.language = "ar_AR" AND l.mode = "learning" AND l.level = 4))
....
ORDER BY isOnline, lastLoginDate DESC
LIMIT 0,10
My questions:
- How can I build the query above with Firebase with the actual structure
- If it's not possible how to improve my database structure for my specific use case (to be able to handle the query above in a better way)