Can i query firestore subcollection by two field - firebase

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.

Related

Query only specific field with firestore

I use this code to get a collection snapshot from Firestore.
firestore().collection('project').where('userID', '==', authStore.uid).onSnapshot(onResult, onError);
This returns a huge amount of data, but I only need a few fields. Is it possible to query only a specific field? For example, if I only need the projectName and the creationDate fields.
Is it possible to query only a specific field?
No, that is not possbile. A Firestore listener fires on the document level. This means that you'll always get the entire document.
For example if I only need the projectName and the creationDate fields.
You cannot only get the value of a specific set of fields. It's the entire document or nothing. If you, however, only need to read those values and nothing more, then you should consider storing them in a separate document. This practice is called denormalization, and it's a common practice when it comes to NoSQL databases.
You might also take into consideration using the Firebase Realtime Database, for the duplicated data.

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)

Firebase how to filter collection with a sub collection

I have a collection called "posts".
Posts has sub collection called "feedback".
when a user give feedback to a post his id and comment get added to feedback sub collection.
Now I want to find posts that user has not given a feedback.
Something like following sql query
select * from posts where userId not in (select userId from feedback)
Can someone provide advice on how to do this?
Firestore doesn't support joins between collections or subqueries. You won't be able to perform any queries that use data from more than one collection.
Also, querying for non-existence isn't supported by Firestore. So, you won't be able to query for the absence of data in a field. Firestore requires that all queries be able to use a highly performant index, which only tracks data present in documents.

Allow users from different collection see a different stream

I have an Orders collection. It contains a field called venueId. And I'm querying against this field using isEqualTo. The venueId is the firebase user uid. I also have a venues collection. It contains this venueId and also has a list of VenueAdmins ids(These ids are also firebase user uids )The app is a point of sales app(pos). I need to query the orders collections so that valueAdmins and venueId see the correct stream. Is quite easy to query with venueId.. venueId,isEqualto, uid. I'm wondering what's the best approach to allow the venueAdmins see the stream as well.
|-Orders // collection
order. //doc
venueId:'2344567788999999'
|-Venues // collection
venue. //doc
venueAdmin: ['3333333333333','55555555555555555']
venueId:'2344567788999999'
My query builder so far: queryBuilder: (query) => query.where('venue.id', isEqualTo: uid)
Firestore does not have the capability to "join" documents from different collections in a single query. A single query can only consider documents in single collection at a time. The way you have your data structured now, it will require at least two queries. First, to find a venue, then second, to find the orders for an admin in a venue.
The only way to make this easier from the perspective of queries is to denormalize your data by duplicating venue data into the order documents. If each order also had a list of admins, then you could reduce this down to a single query.

Flutter firestore: Query Document with 'where' & DISTINCT Document Field

I'd like to query my database so that it returns Distinct Value of the chosen field. (Like SELECT DICTINCT) in MySQL.
Example: I queried my firstore Collection to get Documents where field messageTo isEqualTo 'Terry', this works fine but returns multiple documents with the same field 'chatID'.
I'd like to query the Collection to return Documents where field messageTo isEqualTo 'Terry' and making 'ChatID' field as DISTINCT value.
Firestore doesn't have anything that's equivalent to "select distinct". You will have to make a decision on the client by looking at the returned documents from a query.
It's worth noting, in a more general sense, that Firestore also does not offer any sort of projection view of documents when using client app APIs. Distinct selection is effectively a projection, because it only returns certain fields. Firestore document fetches and queries always return all the fields in a document.

Resources