Below is the Category collection DB structure:
and here is the PRODUCT collection DB structure:
I want to create a search query like similar to AMAZOM were you can just type in "shoes" and get a list of saying men shoes, women shoes, sports shoes etc. or just type in the name of the product and fetch the product.
As per my knowledge of Firestore, I have written a search query were it fetches all the data similar to the inputed letter. For example let's say I type in "S it retrieves the list of product starting with "S", below image is the result of it:
But that is not what I wanted and I have little knowledge of how to construct that kind search query that I want. How can I get my result using Firestore? I can twitch the current DB structure if I can achieve what I want but I don't want to destroy the whole structure too.
You can achieve this by using orderby(), startAt() & endAt() query on Product name.
ref.collection("Product/").orderBy("Product").startAt(text).endAt(text +
'\uf8ff').get();
this will return QuerySnapshot of products where name contains that text.
Related
I am developing a car rental android application in Android Studio. I am working on a "Filter" option, so the users can filter available cars (for example they can choose to see only diesel cars with 5 seats or they can choose to see only all-wheel-drive cars, etc.). I have an activity called FilterCars where the user for example can filter by 4 components: fuel, traction, seats, gearbox. After they submit the filter, the cars are shown based on their preference. I pass those filters to Cars activity(where the cars are shown in a RecyclerView) with bundle extras with success, so I have in Cars.java stored with success user's filter in variables "fuelType, tractionType, seatsType, gearboxType".
With those filters I can pass to the recyclerview adapter a query like:
FirebaseFirestore.getInstance().collection("Cars").whereEqualTo("fuel", fuelType).whereEqualTo("traction", tractionType).whereEqualTo("seats", seatsType).whereEqualTo("gearbox", gearboxType)
which is working just fine.
My problem is the following: I don't want to force the users to complete all the filter fields, so for example, if they want to filter only by fuel and seats, they can do it. How can I make a compound query which only contains the filters chosen by the user? One way I thought about it but I can't resolve it is if, for example, the user doesn't want to filter by traction, to store in tractionType something like " " (empty), and in the compound query instead of tractionType query "whereEqualTo" to put something which returns all the cars, so the compound query will filter only by the chosen filters.
Any suggestion on how to do it? Or is there a better way in which I can avoid putting the empty answers in the compound query?
Thank you!
The following query:
FirebaseFirestore.getInstance().collection("Cars")
.whereEqualTo("fuel", fuelType)
.whereEqualTo("traction", tractionType)
.whereEqualTo("seats", seatsType)
.whereEqualTo("gearbox", gearboxType);
Will filter your collection "Cars" by the value of four properties.
I don't want to force the users to complete all the filter fields, so for example if they want to filter only by fuel and seats
There are two ways in which you can solve this issue. Taking as an example, the above query, you can remove the calls to ".whereEqualTo("traction", tractionType)" and ".whereEqualTo("gearbox", gearboxType)" and you'll have the desired result. Or, you can do as following, as it makes more sense in my opinion.
When the user opens the app for the first time, simply display all available cars that exist within your "Cars" collection:
Query query = FirebaseFirestore.getInstance().collection("Cars")
If there are too many, add a limit() call, or implement pagination. Then simply add the possibility to select the car filters. For example, add 4 check-boxes for each filtering option. Once an option is selected, add it to your Query object like so:
query = query.whereEqualTo("fuel", fuelType)
If the user selects all options, then the query will look like the first one.
Please also remember, that Firestore queries are immutable. For more info, please check my answer from the following post:
FireStore date query not working as expected
If are using for example Firebase-UI library for Android, don't forget to start/stop listening after you set a new Query. If you aren't using the library, don't forget to notify the adapter about the changes.
I am storing some data in firestore and it looks as follows:
What I have is a collection called Items, inside of it I have many documents that each has its own ID and inside each document there are multiple fields.
One of the fields is UserID which shows which user holds the item.
Now, I had like to query firestore in a way that will return me all of the items where their popularity is greater than 3.5 and when my own ID is not one of the ID's shown in the UserID array.
Is there any option to do so?
Currently im using the following:
db.collection( "Items" ).orderBy( "Popularity", Query.Direction.DESCENDING ).whereGreaterThan( "Popularity", "3.5" ).limit( 50).get()
But it also returns results where my ID appears in the array.
Firestore does not offer any queries that check for the non-existence of data in the document. Firestore indexes only work based on the presence of data - you can't efficiently index things that don't exist. So, the part of your requirements that says "when my own ID is not one of the ID's shown in the UserID array" is not possible. You can instead write code in the client app to filter out the documents where the current user's UID is missing.
I'm working on an Android app and I use Firestore to store my data. I have a collection of Users, and every user has a subcollection called Posts. Is there any way to retrieve all posts from all users to show them on my apps feed?
Is there any way to retrieve all posts from all users to show them on my apps feed?
Sure, it is. You need to use collection group queries:
Use a collection group query to retrieve documents from a collection group instead of from a single collection. In code should look like this:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collectionGroup("Posts").get().addOnCompleteListener(/* ... */);
Using the above code, you'll be able to get all Post objects that exist within all subcollections named Posts.
You will want to use a collection group query to get all the documents from all subcollections with the same name. For example:
db.collectionGroup("Posts").get()
There is a "fast" or "less wrong" way to get "dependent" data from Cloud Firestore?
I have two main collections: "users" and "championship".
And a collection named "team" inside every document of a "user".
Every championship is composed of teams, and I'm storing every team in an array of references, like this:
"championship": {
"teams": [
team_1_ref, team_2_ref, ...
]
}
By doing this I can get all teams of a championship, but I also want to get the user owner of the team.
I know that I can create a new reference "attribute" named owner for the team and then get the "data" of every user, but there is another way to do this?
If you're asking if there is a way to perform a "join" type query that spans data from multiple collections, that is not possible. If you have a document with an array, and you want to get all the other documents referred to in that array, you will have to write code to get each one individually. This is normal for nosql type databases, and it's not as bad as you think, in terms of performance.
I am authoring a javabean and would like to know if it should include properties to set values obtained from a sql join?
Say I have database tables for Products and Orders. These are also my java bean names.
Now I want to display a list of all products but in addition to all the product properties, I want to have columns to display last purchase date and last purchased by
My db query to get the product list would need to do joins to gather the additional info. It does not seem correct to have setters and getters for `last purchase date and last purchased by' in Product.java model. I may want to have a 3rd column so constantly adding new columns to my bean doesn't make sense.
How do you go about this? I seem to encounter this when needing to display lists of models in my view.
Have a Map in Product. Upon firing the sql, store hte results of the join as key value pair in the Map. K=lastpurchasedby(columnName) and V= the value
So no need to add individual attributes. They all come in key-value pairs.