I have the JSON structure above
I want to get the list of vehicles where their ids ends with "123"
I had tried to use Query.endAt() method but i'm not sure if i'm using it right or it shouldn't give the required output
Query vehiclesRef;
vehiclesRef = db.getReference("vehicles").orderByKey().endAt("\uf8ff123");
Firebase Database queries can only perform prefix matches, so strings starting with a specific string or starting with a range of string. It is not possible to query for strings ending with a specific value, nor those ending with a range of values.
If you really want to do this within Firebase, consider also storing the reversed strings in the database:
"321_1_0"
"321_3_0"
"654_52_0"
Then you can query for strings starting with 321 with
vehiclesQuery = db.getReference("vehicles").orderByKey().startAt("321").endAt("321\uf8ff");
Related
I want to get sorted data as.... value1--value2--value3....value10--value11
String values are sorted lexicographically. If you want them to be sorted in the order you want, you'll need to ensure that the lexicographical order matches your needs, e.g. pad the numbers in your keys to all be the same length:
"value01": ...
..
"value09": ...
"value10": ...
I wrote a longer answer on this a while ago, see: Firebase query ordering not working properly
I would like to perform wildcard queries on a Firebase database,
and do not know whether the libraries support this. - (my guess it does not.)
I want an optimal solution where the network traffic will be as little as possible, assume the dataset is millions of records.
Dataset (using prefix wildcard):
user_id:
id_001123:
name: "java"
id_002124:
name: "objective-c"
id_003125:
name: "swift"
How would the code look like to retrieve the record id_002124 name field using a wildcard, you only have a portion of the id. eg. "%124".
The expected result would be id_002124, and the name being "objective-c"
Any mobile language example code would great.
(Objective-C/Swift/Java)
Firebase Database queries can order/filter data based on the object's keys (e.g. id_001123), their values (doesn't apply in your case), or the value of a child property (e.g. the fact that name = "java").
For each of these Firebase can match items that have a specific value you specify, items starting at a value you specify, or items ending at a value you specify.
So you can create a query matching the item named objective-c with:
ref.child("user_id").orderByChild("name").equalTo("objective-c")
Or you can create a query matching id_001124 and id_001125 with:
ref.child("user_id").orderByKey().startAt("id_001125")
Or simply getting all items starting with id_0011:
ref.child("user_id").orderByKey().startAt("id_0011")
Or you can create a query matching id_001123 and id_001124 with:
ref.child("user_id").orderByKey().endAt("id_001124")
Firebase Database cannot filter based on the end of a value. So there is no way in your current data structure to get all items whose key ends in a 4.
Please read more about Firebase Database queries here: https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data
And see some of the many previous questions about searching for FirebaseL
How to perform sql "LIKE" operation on firebase?
Firebase query - Find item with child that contains string
Firebase "like" search on string
I have a very similar problem to this post - Firebase Query filtered by creation time and where date is greater than now
I have my dates stored in a "message" and I want to retrieve all messages after the current time.
This query works:
Query myTopPostsQuery = mFirebaseDatabaseReference.child(MESSAGES_CHILD).orderByChild("time");
this query returns no data:
Query myTopPostsQuery = mFirebaseDatabaseReference.child(MESSAGES_CHILD).orderByChild("time").startAt(System.currentTimeMillis());
This seems like it should work but from the docs I'm wondering if this is a data type problem?
In your query, you need to specify the full path to the child used for ordering:
orderByChild("date/time")
You indicate that your first query works. It may return the number of messages you expect, but if you look at them, you will find they are not ordered. The query processing is forgiving. If it doesn't find a value for the child identified by orderByChild(), it assigns a value of null and orders by these rules.
Lets assume I have a List of String (ex: List<String>). I want to match all the Strings in the List to a particular attribute already present in the Firebase Database. How do I achieve this, as the equalTo() query only accepts a single String. I also don't want to generate as many queries as the List size. Any help would be much appreciated.
I've a column/field called last_message_time of type Date in my Table A.
Suppose querying Table A returns x results.
How do i sort these results based on dates inside last_message_time column.
Example, in SQLite we have ORDER BY date(dateColumn)
RealmResults<A> sorted = realm.where(A.class)
.findAllSorted("last_message_time", Sort.ASCENDING);
EDIT: since Realm 4.3.0, the following is preferred:
RealmResults<A> sorted = realm.where(A.class)
.sort("last_message_time", Sort.ASCENDING)
.findAll();
Use just "sort"!
"findAllSorted" is deprecated!
io.realm.RealmQuery.findAllSorted(String)
Since 4.3.0, now use RealmQuery.sort(String) then RealmQuery.findAll() Finds all objects that fulfill the query conditions and sorted by specific field name in ascending order.
Sorting is currently limited to character sets in 'Latin Basic', 'Latin Supplement', 'Latin Extended A', 'Latin Extended B' (UTF-8 range 0-591). For other character sets, sorting will have no effect.
More details in: LINK