Firestore group collection query using Flutter - firebase

I am new in Firestore database. I am using an app using flutter and Firestore. The database structure for my app is like that:
Rooms --Room1-----Reservations.....reservation dates
.....reservation dates
Room2-----Reservations.....reservation dates
Room3
Room4-----Reservations.....reservation dates
Room5-----Reservations.....reservation dates
.....reservation dates
.....reservation dates
'Rooms' is level 1 collection which holds all the room details. Each of the room data holds the 'Reservations' collection to holds all reservation details(check_in and check_out date) for that room. Now I want to get the list of rooms which are available in a specific date span. How to work with 'group collection query' for this requirement? Or it is possible to do the same by 'group collection query'?

Late but, if anyone comes back to this, now in firestore there is something called collectionGroup query where you can get all subcollections if they have same name. Which can be done as
Firestore.instance.collectionGroup('Reservations').getDocuments()
This will give you the list of all the Reservations across all rooms. Please read in detail about collection group query here in the documentation

Now I want to get the list of rooms which are available in a specific date span.
Given that you store reservations, a room is available when there is no reservation for that time. Cloud Firestore cannot query for the absence of data, so in your current model that query won't be possible.
If you'd store the available slots in each room's Reservations collection, you could query across all Reservations collections to get the available slots across all rooms. But note that the result of this query are documents from Reservations, and not the individual rooms; you will need to load each room separately if you need information from that.
Alternatively, you could store a list of still-available slots in each Room document. With that you could do a regular query across all rooms to see which ones are still available, and then perform an update across the Room document and its Reservations collection in a transaction.

Related

How do I retrieve a subcollection of a document in Firestore

I have a Firestore collection with the following structure:
events > default > participants > participant1
> participant2
> date
etc. So events is a collection, and it has a number of documents with random keys (I've used default as an example). Within "default" there are various fields, but also a subcollection of participants.
When I retrieve events/default I expected to retrieve the list of participants at the same time, but I only get back the fields (date etc) - how do I also get the "participants" collection at the same time?
Firestore queries are shallow and do not consider documents in collections other than the one you are querying. There are no SQL-like "joins" that merge documents from multiple collections.
If you query events, then you will only ever get documents in that collection. If you want participants, you will need to make an additional query, using the full path of the subcollection, including its parent document ID. For example, in JavaScript:
firestore.collection("events").doc(knownId).collection("participants").get()

How to turn off automatic indexes in firestore for subcollection

I don't want to have automatic indexes created by firestore because I need to remove and add every five minutes 50-100 documents (each doc has +/-60 fields) to my subcollection. This causes of big volume for "Cloud Firestore Index Write Ops" (300k / day for only one user) and Cloud Storage. I don't need to sort, filtering that documents so I suppose I can turn off automatic indexes, right?
I know that I can add exemptions for fields, but I don't know how can I use it for documents in subcollections. What should I pass in Collection ID and Field path if the path for documents is like:
mainCollectionName/{id}/subcollectionName/{document=**}
and when should I select a collection checkbox and when collection group checkbox?
Unfortunately, it's not possible to disable indexes or create exemptions for documents to be indexed. As clarified in this similar post here, this cannot be achieved and there is even a limit of 200 exemptions of fields that can be done - you can check the limits here.
For your case, indeed, you would have to exempt the fields individually and besides that, to create the exemption, to set the collection you use its id and not the path. So, you would only need to set in the Collection ID field the subcollectionName and then the field to be exempted.
In addition to this, feel free to raise a Feature Request in Google's Issue Tracker, so they can check about implementing an exemption of documents in the future.

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.

Saving users scores and favorites in Firestore Database

I am working in a small project that uses Firestore database as a backend. I explain about the database so it is understood what I need:
Basically I have a collection that contains a list of documents where each one of them represent a game. For each game I have the name, cover image, info, category, etc.
I also have a collection of the users, where I have the specific UID for each user (retrieved from the auth section), email, etc.
What I want now is to save the score that some user may have in some of these games, as well as the favorite games that the user could save. What I don't get to understand is how to create the connection between the users and the games. For example, I thought that I should save the users score creating a collection within each document(game) in the first collection that mentioned. But when I create this collection with ID "scores" it asks me for the first document where I have to facilitate an ID (if not automatic) and then I don't know how to proceed.
I have read also that I would have to create additional collections in the root folder like "favorites" or "scores" specifying the UID of the user but, how do I connect the user UID, the score, and game which the user got that score from?
I hope I explained myself properly. Thanks.
Firstly, I agree with Doug's comment above. The Firestore tutorial videos are a great resource!
In terms of connecting data to your user, you have some options. You can either:
Create sub-collections under each user. Such as /users/{user_id}/favorites. Favorites could be a sub-collection or an array of game_ids depending on your use case.
Store a userID field in the documents in a top level "scores" or "favorites" collection. Then you can query for scores in the /scores collection by adding a where userID == {user_id} clause to your query of the /scores collection.

How to get a parent collection by filtering data in a subcollection within the parent collection in firestore?

I'm developing a project with firebase, and I'm having trouble searching for a specific parent collection by filtering on one of its subcollections.
For example, when starting a trip, I must check which students (parent collection) have their presence confirmed (the going attribute must be true) and also the date of attendance should be equal to the date of the trip. How can I search for confirmed students (which would be the parent collection) when filtering data in the presence subcollection?
You would need to make these calls separately as there's no JOIN-like call in Firebase. In other words, you can't reference a document to call another.
To achieve it, you would need to consider something like querying all students who have going true and collate it will the results of the date of attendance match query.

Resources