I started developing simple app to learn Firebase, I followed cool blog post: https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html
In my app, I want to store user profiles (its extension of firebase user) and relationship between users (something like friendship)
I came up with this data structure idea:
profile
profile1:
userName:"User 1",
userDescription: "User 1 description"
profile2:
userName:"User 2",
userDescription: "User 2 description"
profile3:
userName:"User 3",
userDescription: "User 3 description"
profileFriends:
profile1:
profile2: true
profile2:
profile1: true
profile3: true
profile3:
profile2: true
Of course instead of profile1 I use pushed keys.
I wonder I its okay for such a use case - I want to display all friends of profile2.
I have to get Database reference to
"profileFriends/profile2"
And then iterating childs gives me keys: profile1 and profile3 which I can then listen using reference
"profile/profile1"
"profile/profile3"
Since Im working in Android, I can wrap all this code and use Observable that emits profiles.
Question: Do I get this right? I have some SQL background and standard request-response api experience, Im just little worried if my user have 100 friends Ill need to make total 101 listeners - is it similar to make 101 requests? Is there any smarter way to solve "join" in non-joinable no-sql database?
I guess another solution is denormalization, but I'm not a big fan of updating many places to change for example profile description
Related
I have a question to the Firebase community.
How to structure the data for # pages like Twitter? (e.g:#funnyVideos so that any one can see that link and add some stuff into it) in Firebase?
It sounds like you're trying to store hash tags.
But that enough is not enough to determine your data model.
In Firebase (as in most NoSQL databases) you model your data for the way you app consumes it. So you'll have to determine what screens there are in your app and what information they show.
For example, if you want to show the hash tags per page, you'd store a list of hash tags per page:
hashtagsPerPage
pageId1
hashTag1: true
hashTag2: true
pageId2
hashTag1: true
hashTag3: true
If you also want a list of pages for a given hash tag, you'd also store:
pagesPerHashtag
hashTag1
pageId1: true
pageId2: true
hashTag2
pageId1: true
hashTag3
pageId2: true
And of course you'd then have the list of pages itself:
pages
pageId1
title: "Creating # pages like Twitter with Firebase"
url: "http://stackoverflow.com/questions/39947270/creating-pages-like-twitter-with-firebase"
pageId2
title: "Structure Your Database"
url: "https://firebase.google.com/docs/database/web/structure-data"
I highly recommend that you read both the Firebase documentation on data structuring (the first two lists are the structure documented in the section "create data that scales").
In addition there's this great primer on NoSQL data modeling.
I have a quick question about the best practices for data structure in a firebase database.
I want users of my app to be able to maintain a friends list. The firebase documentation recommends creating a schema (not sure if thats the proper word in this context) that is as flat as possible. Because of this I thought it would be a good idea to separate the friends section from the player section in the database like so:
{
"players":{
"player1id":{
"username":"john",...
},
"player2id": ...,
"player3id": ...
}
"friends": {
"player1id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
"player2id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
}
So my questions are as follows:
Is this a good design for my schema?
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Is this a good design for my schema?
You're already thinking in the right direction. However the "friends" node can be simplified to:
"friends": {
"player1id": {
"friend1Id":true,
"friend2Id":true
}
}
Remember that Firebase node names cannot use the character dot (.). So if your IDs are integer such as 1, 2, and 3 everything is OK, but if the IDs are username be careful (for example "super123" is OK but "super.duper" is not)
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
No. If you pull /friends/1 it obviously won't pull /friends/2 etc.
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Loop through the IDs and fetch the respective nodes from Firebase again. For example if user 1 has friends 2, 3, and 4, then using a for loop fetch /players/2, /players/3, and /players/4
Since firebase pull works asynchronously, you might need to use a counter or some other mechanism so that when the last data is pulled you can continue running the completion code.
I want to create a simple search in my app, but cannot find anything on interwebs about it, that's more recent than 2014. There must be a better way. There are startAt and endAt functions but they don't work as expected and are case sensitive. How do you guys solve this problem? How can this functionality still not exist in 2016?
In my case I was able to partly achieve a SQL LIKE in the following way:
databaseReference.orderByChild('_searchLastName')
.startAt(queryText)
.endAt(queryText+"\uf8ff")
The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText.
In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName property from the database.
Create two String variables
searchInputToLower = inputText.getText().toString().toLowerCase();
searchInputTOUpper = inputText.getText().toString().toUpperCase();
Then in the Query set it to:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Products");//Your firebase node you want to search inside..
FirebaseRecyclerOptions<Products> options =
new FirebaseRecyclerOptions.Builder<Products>()//the Products is a class that get and set Strings from Firebase Database.
.setQuery(reference.orderByChild("name").startAt(searchInputUpper).endAt(searchInputLower + "\uf8ff"),Products.class)
.build();
the "name" it's the node inside the Products Main Node.
the .startAt(searchInputUpper) & .endAt(searchInputLower + "\uf8ff") to make the search as contains all characters that typed in the inputText.getText() that you get.
finally I got it you can use where clause to get you result like SQL
LIKE keyword like% or %like
syntax :
Firestore.collection(collectionName).orderBy(field).where(field, ">=", keyword.toUpperCase()).where(field, "<=", keyword.toUpperCase() + "\uf8ff").get()
I my case used:
var query = 'text'
databaseReference.orderByChild('search_name')
.startAt(`%${query}%`)
.endAt(query+"\uf8ff")
.once("value")
In this way, searching by "test" I could get the records having "Test 1, Contest, One test" as value in 'search' property from the database.
Firebase is noSQL therefore it does not have searches built in like you'll find in SQL. You can either sort by values/key or you can equalto
https://firebase.google.com/docs/database/android/retrieve-data
You can find examples at the link above. That is the latest documentation for firebase.
If you are looking for SQL like searches. Then take a look at elastic search. But that will increase the complexity since you need a platform to put it on. For that i could recommend Heroku or maybe GoogleCloudServers
Here is a blog post about advanced searches with elastic search
https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html
This question might be old but there is a documented way of how to achieve this way, It is simple to implement. Quoted:
To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document:
Algolia will be part of your firebase functions and will do all the searches you want.
// Update the search index every time a blog post is written.
exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(event => {
// Get the note document
const note = event.data.data();
// Add an 'objectID' field which Algolia requires
note.objectID = event.params.noteId;
// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(note);
});
To implement the search, the best way is to use instant search - android
Sample Search Image: GIF
The feature you're looking for is called full-text search and this is something most databases (including Firebase) don't provide out-of-the-box because it requires storing the data in a format that's optimized for text search (vs optimized for filtering) - these are two different problem sets with a different set of trade-offs.
So you would have to use a separate full-text search engine in conjunction with Firebase to be able to do this, especially if you need features like faceting, typo tolerance, merchandizing, etc.
You have a few options for a full-text search engine:
There's Algolia which is easy to get up and running but can get expensive quickly
There's ElasticSearch which has a steep learning curve but uber flexible
There's Typesense which aims to be an open source alternative to Algolia.
I don't know about the certainty of this approach but using the firebase version 10.2.6 on android, i get to do something like this:
firebaseDatabase.getReference("parent")
.orderByChild("childNode")
.startAt("[a-zA-Z0-9]*")
.endAt(searchString)
It seems to work well sometimes
Finally joined SO just to answer this.
For anyone coming here from/for the python firestore.client here's a solution that seems to work for me.
It's based on the accepted answer's concept but via the client rather than db.reference() and mixed with the answer from user12750908.
from firebase_admin import firestore
users = db.collection("users")\
.order_by("last_name")\
.where("last_name", ">=", last_name.upper())\
.where("last_name", "<=", last_name.lower() + "\uf8ff")\
.stream()
It works for the simple test I did, but I'll update my answer if I have issues with it later. And just a reminder, this is similar to
LIKE search%
and not
LIKE %search%.
Edit 1
I didn't see any tags for the question, but the title attribute mentions Android so this may not necessarily answer the question directly, but if you have a python API, this should work. I'm unfortunately not sure if there's an equivalent client/db separation in the Android version like there is in the Firebase Admin for Python. I didn't want to delete the answer since I hadn't seen any answers for firestore client during my search for a similar answer and hope it helps anyone else stumbling around.
Edit 09-03-2020 This works a portion of the time it seems. Most of the time I didn't seem to have an issue, but when I applied it to another project I was getting unexpected results. Long story short you may need to replicate how you save the data you're comparing against. For example, you may need to have a field to save the last_name in all caps and another field to save it in all lowercase, then you change the first where clause to compare last_name_upper and the second to compare last_name_lowercase. In my second project so far this seems to yield more accurate results so you may want to give that a try if the previous answer doesn't work well
EDIT 09-07-2020 Previous edit from 09-03-2020 is partially accurate. During my haste of thinking I had it fully resolved I completely forgot firebase doesn't let you use <, >, <=, >= across different fields. You may need to do two queries and merge them, but you'd probably still be reading more docs than you really intend. Doing the comparison against either the upper or lower version with the appropriate search term seems to give the original results expected from the original answer. For example:
.orderBy("last_name_upper")
.where("last_name_upper", ">=", this.searchForm.text.toUpperCase())
.where("last_name_upper", "<=", this.searchForm.text.toUpperCase() + "\uf8ff")
As firebase documentation, firebase doesn't support full text search.
But to do that you can use third-party tools.
Check this link to learn more https://firebase.google.com/docs/firestore/solutions/search
I'm quite new to Android programming (very little programming experience). I want to make an app that will track Car maintenance. I would like users to be able to see their data (roughly) according to the following hierarchy:
Year (see total costs, maybe summarize categories)
--Month (month's costs)
----Maintenance Instance
------Details about the instance (what was done for what cost)
I don't have my data design finalized, but you can see the kind of data I'm trying to track. What approach would you suggest? Do I need to use SQLite? If so, would you recommend a hierarchy of tables or just one table that will be shown hierarchically through queries? Like I said, I'm new. I'd appreciate any pointers in the right direction.
In Android, you can use SharedPreferences to store simple data like global preferences (i.e. in your app you could store a currency flag as a preference to display currency as dollars or pounds) but for anything more complicated you should use SQLite. This tutorial is excellent and will get you started - http://www.vogella.com/tutorials/AndroidSQLite/article.html It seems like you could have one table with each row being a maintenance entry with columns for the date, cost and action carried out. You could then query the database by a date range to get the cost for that range or a list of action carried out in that range (e.g. per month or year). Each row would represent a separate maintenance event.
I recommend you use JSON, a very easy to use storage format. A typical JSON message you would store might look like the following:
{
"maintenance_data": [
{
"date": 1091029109,
"maintenance_details": "Drove car around while owner was gone"
},
{
"date": 1021234134,
"maintenance_details": "Ate cookies while on job"
},
{
"date": 1041023234,
"maintenance_details": "Ain't nobody got time for maintenance"
}
],
"car_id": 1234,
"owner_name": "Slick diddy"
}
In an Android app i have implement LimeSurvey,in which list of different Survey are displayed from lime survey admin panel.
I am able to display questions of a survey but not getting any structure to submit user answer's of survey to LimeSurvey database.
Any one can please help me how can i submit answer and in what structure lime survey accept response of survey.
As document suggest to add response in format -
sSessionKey (string) - The session key
iSurveyID (integer) - The survey id
aResponseData (array) - array/structure containing your response data
so my query is what the structure for aResponseData (array) , so it properly store in lime survey database.
Edit:
How to add answer of sub-questions.I am able to submit answer of simple question but how to send answer of subquestion and in Survey question type-L(List of radio with comment)how to submit comment.
Thanks in advance.
The only way I found to solve this was downloading the .sql database backup from the main taskbar and look for your table, which is *lime_survey_IDSURVEY*, and check for the columns which match with your questions, and use them for your mapping in the aResponseData. I'll leave you an example of that:
{"method": "add_response", "params": {"sSessionKey ": "ey2bv8v7zaw7uwj6fa9a8mmx9xqiikja", "iSurveyID ": 588636, "aResponseData ": {"588636X1X1":"Aja","588636X1X22":"Y" } }, "id": 1}
This will be the JSON that I send with my POST request, where 588636X1X1 is a column that match with a question, and "Aja" is the answer (in that case is a text-string), meanwhile, the other one, it's the same but it's a multiple choice question. As you can see, there is a pattern for the column names, 588636 it's my survey ID, X1 it's the group of the question, and X1 it's the ID for my question.
Hope it's useful.
You should be using the limesurvey remote control feature to call the webservices api for various tasks you want to perform: add_response in this case . Its documentation is here
--
amit