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()
Related
There is a way to batch get documents after you specified exactly all the file paths using
https://firestore.googleapis.com/v1/projects/projectName/databases/dbName/documents:batchGet.
And there is a way to get all documents and their fields under a collection by sending a GET to https://firestore.googleapis.com/v1/projects/projectName/databases/dbName/documents/collectionName
I ideally want to make a batch request to get document fields for all documents under an array of collections. Is there a way to do this without knowing the document names of every document I intend to get?
Example
I have a structure like projects/projectName/databases/dbName/documents/Inventory/productId/variantId/*location*
Each productId is a document, and under this it has a collection for each variant, and within that collection are documents for each location, that contains a field count.
For a basket, I want to get all inventory counts for all inventory locations, for each productId/variantId in that bas
It is not possible to get all documents based on an array of collection names.
You can use a collection group query and search all collections of a given name, but then you must know the path of each document you want to read.
Alternatively, you can get all documents under a specific path, but then you can't filter by ID anymore, and the collections have to be under a path - not an array.
Is it possible to fetch a document from a subcollection without parent document ids?
We have a structure like:
RootCollection1
-- SubCollection1
-- SubCollection2
- Document1
-- SubCollection3
-- Document2
-- SubCollection2
-- Document3 [different fields with Document1]
App User will only have the Document1 ID.
I tried implementing a collection group query. Notice that SubCollection2 name appears twice and they have different fields/values. I only want to get the document from one SubCollection2.
Is it possible to fetch the data from document1 without the parent doc id from SubCollection1 and RootCollection1.
If you can't locate a document using its full path (knowing the names of all documents and collections in the path), and you can't use a collection group query, then what you're trying to do isn't possible. You can't focus a collection group query to a specific path - they always consider all documents in all collections with the same name.
You have two viable workarounds:
Change the name of the subcollection so that it's unique and does not overlap with subcollections that should not be queried in a collection group query.
Add a field to the document so you can use it in a filter on a collection group query. You will have to be able to identify which documents you are interested in without considering all documents in all subcollections with the same name. For example, if you have a field called "scope", you can narrow the scope of your collection group query like this:
firestore.collectionGroup('coll').where('scope', '==', 'x')
Or you can store the ID of the document as a field in it, and filter for it:
firestore.collectionGroup('coll').where('id', '==', 'id')
This is all you need:
firestore.collection("Task").doc(taskId).collection("SubTask").doc(subTaskId).get();
Consider the following Firestore structure:
Collection people
Every document in the collection has a subcollection pets
Subcollection pets has multiple documents with pet data.
Now, I want to query for people whose pets satisfy certain conditions. Such as, people who have more than two pets, or who own a pet whose name is "ABC".
Is this possible?
The first example query you listed ("people who have more than two pets") is not possible with your current set of data. There are no counting or aggregate operations provided by Firestore. If you want to count things, you will need to get all the documents and count them on the client, or maintain an active count using some other means.
The second example query you listed ("people who own a pet whose name is "ABC") is possible through a collection group query against the subcollections named "pets" that filters documents using a field that contains the name of the pet. The query will return all of the pets among all of the subcollections, and you will have to iterate them and examine the references to those documents to build the list of people. With your data, it's not possible to issue a single query to get only the list of people.
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.
My firm asked me to get all the documents from the root collection, users. They must be sorted according to the value of a numeric field, titled amount, present in a first sub-level document and also in a second sub-level document.
I don't know how to fulfill this aim easily with Firestore NoSQL queries. What would you recommend to me?
Data structure
Here it is:
users
user_1 (document to get and sort)
user_2 (document to get and sort)
Each of these two users is structured like this:
collection_1
document_of_first_level
contains this field: amount
and contains also this collection: collection_2
document_of_second_level
contains this field: amount
So I should get a list containing user_1 and user_2. This is a sorted list. It's sorted according to the two fields both named amount, which are contained under user_1, but also under user_2. These both fields are nested.
They must be sorted according to the value of a numeric field, titled amount, present in a first sub-level document and also in a second sub-level document.
As also #DougStevenson mentioned in his comment, you cannot achieve this with Cloud Firestore. There is no way to get documents from a subcollection (level 1) and another subcollection (level 2) in a single query. Firestore doesn't support queries across different subcollections in one go. Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. A single query may only use properties of documents in a single collection.
So the most simple solution I can think of, would be to query the database twice, once to get the documents within subcollection (level 1) and second to get documents within subcollection (level 2) and then compare them client side.
The idea from your comment is a solution since adding a property under the level 1 document will allow you to query the database just once. In Firestore, you can simply chain multiple where() functions in a single query.