Get parent collection & sub collection document on firebase - firebase

I'm new to firebase I had 2 collection categories & products and the product collection, has a sub-collection Category, how can I get all documents from the product collection and sub-collection category? thank in advance

There is no way to read from both the parent collection and the sub collection in one operation. Read always come from one (type of) collection, which is sometimes explained as "all reads in Firestore are shallow".
You can either:
Read the parent collection, and then for each (relevant) document read its subcollection as a separate operation.
Read the parent collection, and read all SubCategory collections in one go with a collection group query.
The second approach performs fewer calls to the server, but has a higher chance of reading more documents than needed, if (for example) you may not want the SubCategory collection from some documents.

Related

Firestore - Batch querying nested documents in REST API

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.

delete a collection and all the including data (collections and fields)

Im trying to delete a document X in collection Users but in this collection, there is another collection tasks is the collection Users. How can I delete all the data in the document X (includes collection tasks)
firebase.firestore().collection("Users").doc(X).delete();
There is no single operation in the API to delete an entire collection. Instead a collection comes into existence once a first document is added to it, and a collection disappears once the final document is deleted from it.
This means that you'll need to delete each document in the collection individually, preferably using batch delete operations. Once you've deleted the last document from the subcollection, the collection itself is also gone.

how do I do a collectionGroup query but get the documents that subcollection is inside of?

how do I do a collectionGroup query but get the documents that subcollection is inside of. My data is structured as: Users (collection) -> UID(doc) -> privateData (collection) -> UID (doc)
and I want to query based on a field in the UID docin the privateData collection, but I want the actual data from the UID Doc in the Users collection
You can't query across multiple collections like this. If you want data from a different subcollection, then your collection group query will need to target that other subcollection instead. This means you would probably have to duplicate the fields from privateData collection into the Users collection. Duplication of field data is common in nosql databases like Firestore.
If you can't duplicate that data for whatever reason (such as privacy), then you would need to query each related document separately, effectively joining each users doc to the matching doc in privateData.
Depending on your data model, you would have to have a reference to the parent document for the sub collection in the child object, or fetch the document and using the documents snapshot, you can call its parent reference
something like this: querySnapshot.docs()[x].snapshot.getRef().getParent()

How to get documents from Firestore without a single document id

So i have a collection named products in my Firestore and it contains all my products.
Since i am using Tag widget it gives me an error when there is more than one item sharing the same tag. So for my Products Builder i want to give a list that contains all my products without a specific product catched by his id, so stream property of my Stream Builder can be like this.
StreamBuilder(stream: Firestore.instance.collection('products').WITHOUT SPECIFIC PRODUCT ID .snapshot()
builder:...)
How can i do that?
It's not possible to construct a query for all documents except one. There are no inequality filters in Firestore.
Since it's just one document, it makes sense to simply do the query for all documents, the have the client code remove the one you don't want from the result set.

Firestore: Getting documents sorted by a numeric field contained in the two sub-levels documents

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.

Resources