Firebase query for multiple where condition - android

I need equivalent query of
SELECT PlayerName FROM GameScores
WHERE Department = 'HR' OR Department = 'IT' OR Department = 'Marketing'
In Firebase for android , I don't want multiple call, just one request and get all data that match with where clause key. How I can do it?

The only way to filter a property on multiple values in Firebase is if those values are in a range. For example, you could get all items matching your values with:
ref.child("GameScores")
.orderByChild("Department")
.startAt("HR")
.endAt("Marketing")
The problem with this is that it also matches scores from other departments whose name is between HR and Marketing, such as
There is no way in either the Firebase Realtime Database or Cloud Firestore to pass multiple separate values into a query. You will have to execute a separate query for each value and merge them client-side.
Also see:
firebase equivalent to sql where in ()

Related

Firebase Firestore OR query (Android Studio)

I've been trying to find a query for almost 2 days now
I want to search id (current user id) from the document 4 fields (customer1,customer2,customer3,customer4)
Here is the firestore document picture
tried this query
final Query userQuery = collectionReference
.whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());
but this only shows up if the current ID is present in all 4. Is there any easier way to do this.
You can do that by using a field that is an array containing the uids you want to test, and then applying array-contains on it. In your case:
In your case:
customer: [customer1, customer2, customer3, customer4]
collectionReference
.where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
Firestore does not support logical OR queries among mulitple fields. So, what you're trying to do is not possible with a single query using the database structure you have now. You would have to perform multiple queries and merge the results in the client.
If you want to be able to use a single query, you will have to change your database. One option is to put all the customers into a single array field and use an array-contains query to find a customer in that field.

Firebase matching substring

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");

Firebase Database - How to perform a prefix wildcard query

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

Compare a list of String values Firebase RealTime Database

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.

ActiveAndroid: Subqueries?

I'm using ActiveAndroid in an app for database interaction. I need to join a few tables and then filter the results, which I would usually accomplish using a SQL query with a sub-query.
How might one accomplish a sub-query using ActiveAndroid?
If a sub-query cannot be done, is there a way to only return unique Model objects? The join in my query has the effect of creating duplicate rows in the join table, but I only want one instance of each Model to be returned.
EDIT: Providing more info
The model I'm trying to retrieve is called Profile.
With regard to the query, there are 3 tables:
Profiles, Roles, and ProfileRoleJoins
There is a many-to-many relationships between Profiles and Roles and that relationship is maintained by the join-table called ProfileRoleJoins.
My goal is to obtain all Profiles that have a certain "event_id" and are connected to one or more Roles specified by a list of Role types.
Of interest to this query are the following fields:
Profiles
Id
event_id (not a foreign key, just a data field)
Roles
Id
type (TEXT)
ProfileRoleJoins
profile (foreign key)
role (foreign key)
What I've Tried
String eventId = //provided as input to this function
String[] roles = //array of "types" of roles we want to limit to
String whereClause = //built in a loop, looks like "Roles.type = ? OR Roles.type = ?..."
new Select()
.from(ProfileEntity.class)
.where("event_id = ?", eventId)
.join(ProfileRoleJoinsTable.class)
.on("Profiles.Id = ProfileRoleJoins.profile")
.join(RoleEntity.class)
.on("ProfileRoleJoins.role = Roles.Id")
.where(whereClause, roles)
.execute();
The above query has the problem that it returns duplicate Profiles because a single Profile can be associated with multiple Roles which creates duplicates during the join process.
I need to retrieve a list of unique Profiles.
I tried using Distinct(), but that qualifier applies per set of columns - I need entire Profiles back.
The way I would normally accomplish the uniqueness is by doing the above query in a sub-query and return only DISTINCT Profiles.Id values. Then I would run a filter in the main query against the original Profiles table.
I still don't know how to do sub-queries with ActiveAndroid (if its even possible), but I found out the source of my problem. I was using an outdated version of the library and there was some kind of error in the query system that screwed up the results. With the current version of the library I do not get duplicates.

Categories

Resources