Assuming I have a list of data I would like to store with Firebase realtime database, and search it later.
What would be the best way to store the data and query it to g
If the names are supposed to be unique and order doesn't matter, you'll want to store them as a mathematical set. In the Firebase Realtime Database you'll model this as:
"usernames": {
"Bob": true,
"Rob": true
}
A few things of note about this model:
true
values have no specific meaning. They are just needed, since Firebase can't store a key without a value..
and /
) cannot be used in keys. If a name contains such characters, you will have to filter them out (or encode them) in the key. For example someone named Jes.sie
will have to be stored as Jes.sie
(lossy) or e.g. Jes%2Esie
(with URL encoding)."Jes%2Esie": "Jes.sie"
.A few more general notes about (text) searching in the Firebase Realtime Database:
B
(with orderByKey().startAt("R").endAt("R\uF7FF")
), but it can't search for everything ending with ob
.Searches are case-sensitive. If you want to be able to search case-insensitive, consider storing the keys as all-lowercase:
"usernames": {
"bob": "Bob",
"rob": "Rob",
"jes%2esie": "Jes.sie"
}
If you need better support for text-search, consider integrating a third-party search engine. Common recommendations are Elastic-search (self-hosted) or Algolia (cloud-based).
For more information on many of these topics, see: