Is there any way to use OR query with Firestore RecyclerAdapter? - android

I have a recyclerview using Firestore Recyclerview Adapter, Now I want to show the list by using OR query.
For example I want to show the list which are either in "Pending" state OR "Assigned" state using query. I know firestore doesn't have OR query inbuilt but I want to achieve this anyhow. Any alrernative solution.
I have stored 4 state in firestore db : Pending, Assigned, Accepted and Completed.
Only want to show pending or assigned
Below is my code:
Query query = requestVehiclesCollectionReference
.whereEqualTo("clientId", id)
// show list which are in Pending state or Assigned state
.whereEqualTo("status", "Pending")
.orderBy("createdAt", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Item> options = new
FirestoreRecyclerOptions.Builder<Item>()
.setQuery(query, Item.class)
.build();

Firestore doesn't support queries with OR conditions. See:
Implementing OR in firestore query - Firebase firestore
How to perform compound queries with logical OR in Cloud Firestore?
Firebase Firestore - OR query
The common workaround is to use multiple queries (one for each condition) and merge the results in your application code.
FirebaseUI's adapters show data from a single query or collection. They have no option to show the results from multiple queries/collections.
From this combination it follows that you can't show data from an OR condition with a single FirebaseUI adapter. You will have to create you own adapter for this, although you can certainly use the (open-source) FirebaseUI adapters for inspiration.

Related

Need a query out of multiple collections from Firestore

I really can't find how to do it, how do I get in my RecyclerView all the values of a certain department.
First things first, is it even possible?
This is how my Firestore database loos like this, "blue_bottle" and "tops_national" is part of a document:
I want to populate my RecyclerView with all products, both out of "blue_bottle" and "tops_national" with the same department.
So I need a DocumentReference which I believe is right:
private val promoOnedb = FirebaseFirestore.getInstance()
private val promoOneRef: DocumentReference = promoOnedb.collection("tops")
.document("promotions")
But how do I query that DocumentReference now to show all products, in both collections to show all products of the same department? Please.
The only way in Firestore to read across multiple collections is with a collection group query, which reads from all collections with a specific name.
o with a single collection group query you can read from all blue_bottle collections or all tops_national collections. There is no feature in Firestore however to read from all blue_bottle collections and all tops_national collections with a single operation. You will need at least two (collection group) queries for that, and thus merge the results from those in your application code.
If you want to read all products with a single operation, they will need to be stored in collections of the same name, like products. Only then can you use a single collection group query to read them all in one go.

Fetch SubCollection data into Recyclerview

I'm trying to retrieve data from a Firebase sub collection in a recyclerview. I think it must be possible but what I did was not right I think
I want to recover the data from REPARS_TABLE in Recyclerview
Android - Cloud Firestore
Query query = db.collection("RESTO_TABLE").document().collection("REPARS_TABLE").orderBy("nomRepars");
FirestoreRecyclerOptions<ReparsModele> OptionRepars = new FirestoreRecyclerOptions.Builder<ReparsModele>()
.setQuery(query,ReparsModele.class)
.build();
Log.d("SHOW","SHOW QUERY"+ query);
bureauReparsAdapter = new BureauReparsAdapter(OptionRepars);
repaLists.setHasFixedSize(true);
repaLists.setLayoutManager(new LinearLayoutManager(getContext()));
repaLists.setAdapter(bureauReparsAdapter);
bureauReparsAdapter.notifyDataSetChanged();
You will need to call out the ID of the document whose subcollection you want to you. Right now, you're calling document() with no parameters, which means it's generating a random ID as part of the path. You will need to instead pass it the ID of the document.
If you don't know the ID ahead of time, then you won't be able to do this query, or you will have to use a collection group query and filter the documents you want from all of the subcollections named "REPARS_TABLE".

firebase query how can i retrieve two specific values from child

i use recyclerview to retrieve a chat messages and i am wondering if there is method to retrieve two different values i mean something like this :
FirebaseRecyclerOptions<enchat> options =
new FirebaseRecyclerOptions.Builder<enchat>()
.setQuery(
mChatDatabase.orderByChild("state").equalTo("sent")
&& mChatDatabase.orderByChild("state").equalTo("received"),
enchat.class)
.build();
There is no way to pass two values into a condition. Firebase Realtime Database doesn't support this type of OR condition.
Also see:
Query based on multiple where clauses in Firebase
iOS - OR query in Firebase
The common workarounds are:
Perform two queries and merge the results in your application code. But you won't be able to use the adapters from FirebaseUI as is in that case.
Perform a range query, but this only works if there are no state values between received and sent. If that is the case, the query could be:
mChatDatabase.orderByChild("state").startAt("received").endAt("sent")
Add an additional property that is true when the state is either received or sent and filter on that:
mChatDatabase.orderByChild("stateIsReceivedOrSent").equalTo(true)

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.

Android's Firestore to join 2 collections into a recycler view

I have a users collection with uId, name, photo
I have a visits collection with uId, userId, location
I have a recyclerview in which I want to show the location with the user name and photo
Can I use the reference field type? If so, how will Firestore know to link visits.userId == users.uId ?
Maybe I first need to query all the visits and then query the relevant user but 2 things:
It means querying a lot of times.
I didn't understand how to collect the joined collection into the adapter, which is based on one query?
Please advice
Thanks
current code
visitsList = db.collection("visitsList");
Query query = visitsList.whereEqualTo("userId",prefs.getString("id","")).orderBy("visitDate", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<AVisit> options = new FirestoreRecyclerOptions.Builder<AVisit>().setQuery(query, AVisit.class).build();
adapter = new VisitsListAdapter(options, VisitsListActivity.this);
RecyclerView rv = findViewById(R.id.rvVisitsList);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(adapter);
The code is a simple query from the collection, not sure how to get the name and photo from the userId field in that collection.
Can I use the reference field type?
Yes, you can use a reference field.
If so, how will Firestore know to link visits.userId == users.uId ?
Firestore results always comes from a single collection (at the moment). It does not automatically join the document from the users collection when you're reading from the visits collection. You will have to do this yourself.
That indeed means you'll be executing multiple reads, but it's often not nearly as slow as you may think. See Google Firestore - how to get document by multiple ids in one round trip?
Update: To show data from the user profile in a list of visits, there are two main options:
load the additional user document in populateView or with a custom parseSnapshot implementation.
duplicate the relevant user data in the visits collection (which is quite normal in NoSQL databases). Also see Alex' answer here: indexed query with FirestoreRecyclerAdapter.

Categories

Resources