firestore REST order by 2 fields ( composite query ) - firebase

the firestore REST documentation doesn't mention info about compound queries
https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/list
How can I order by 2 fields when querying a list of documents ? ( for example, I want the student documents to be ordered by the student school ID then by the student average score )
do I need to manually generate the composite index ( the sdks provide you with a url to generate the required composite index )?
Thanks

somehow I missed it in the docs, here is the syntax for composite queries, simply separate the fields by a comma
https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/listDocuments
cities?orderBy=likeCount, likeAverage

Related

search a doc with its ID in 2 different Firestore collection at backend in flutter

I have the ID of the DOC, is there any query to search that doc in 2 different collection, am doing this check in my code but is there any firebase query to do same.
If the collections that you want to search have the same name, you can use a collection group query to search both of then (well actually, you'll be searching all collections with that name).
If the collections have different names, there is no way to search them both with a single query. You'll have to do a separate query for each collection, and then merge the results in your application code.

how to query nested maps inside an array field in firebase

I have structured my data in Firebase such that I have a collection of 'users' and each user has various fields as well as a 'skillsIndustry' Array field which contain Maps items (that have fields 'experience' and 'industry').
Now I would like to query my collection of users so that I can ask for all users that have worked in 'Airlines' with experience code of >= 1
Is this possible at all? Or do I have to create sub collections and then use Collection Group Queries?
Many thanks
No, this is not possible. You can use array-contains to query for the entire object, as explained in this SO answer, but not to query with > or <.
You could create a subcollection, but you can probably find a solution based on the duplication of the data in the same document, which is a common approach in the NoSQL world.
For example, you could have some extra fields named airlinesExperience, shippingExperience, etc. Then you use the skillsIndustry Array field when you want to display all the skills and years of experience, but you use the xxxExperience fields when you want to query.
Another possibility would be to use a map of key/value pairs like
key = "Airlines" / value = 1
key = "Shipping" / value = 3
Then you can query with:
firebase
.firestore()
.collection('users')
.where('skillsIndustry.Shipping', '>', 2)

Query based on referenced document's field

I have 3 collections
Companies: [{name:'Lamy'}]
Pens : [name:'Safari', company:ref::companies]
Writing Samples :[{title:'Post title', pen:ref::pens }]
I want to get all writing samples from from a particular pen company.
Is it possible to filter based on field values in a referenced document or do I need to restructure my firestore?
Example
const company = db.collection('companies').doc(form.penCompany);
db.collection('samples').where('pen.company', '==', company)
My guess is no and that I need to restructure my DB to include the company information nested in the sample.
You will need to restructure. A query on a collection can't reference documents from other collections - there are no SQL-like joins. Everything in the query must be in the same collection. This means you might have to duplicate data between collections. This is a common technique when working with NoSQL. It's call "denomralization".

Firebase queries and compound indexes

I have the following document structure in firebase:
{
typeId: number,
tripId: number,
locationId: string,
expenseId: number,
createtAt: timestamp
}
I want to query this collection using different 'where' statement everytime. Sometimes user wants to filter by type id and sometimes by locationId or maybe include all of the filters.
But it seems like I would need to create a compound index of each possible permutation? For example: typeId + expenseId, typeId + locationId, location + expenseId, etc, otherwise it doesn't work.
What if I have 20 fields and I want to make it possible to search across all of these?
Could you please help me to construct a query and indexes for the following requirement: Possibility to query across all fields, query can contain one, two, three, all or no fields included in where clause and always has to be ordered descending order by createdAt.
Cloud Firestore automatically creates indexes for the individual fields of your documents. So it can already filter on each field without you have to manually add these indexes.
In many cases it is able to combine these indexes to allow queries on field combinations, by performing a so-called zig-zag-merge-join.
Custom additional indexes are typically only needed once you add an ordering-clause to your query, in addition to filter clauses. If you have such a case, the Firestore client will log an error telling you exactly what index to create (with a link to the Firestore console that is prepopulated to created the index for you).

Can i query firestore subcollection by two field

For example, do a query where due date is today and createdby some user ?
Yes, you can query any collection or subcollection using two (or even more) fields as filters. You should take a look at the documentation.

Resources