[![I want a booking system like as collection->appoinment_user_id->user_id(Who want to book)->date and time.][2]
There is no API to get all subcollections under a document in the client-side SDKs for Firestore. The common pattern is to give collections known names, which you can type in your code, and only use generated IDs for documents.
So in your scenario you'd add a users collection under the 0Sj3n.... document, and then under that you'd add documents for the user IDs again.
Also see:
How to list subcollections in a Cloud Firestore document
How to get names list of collection in firestore?
Related
Is it possible to get data of sub-documents by reading only the main document in Firestore? Basically, the Firebase listCollections() method gives the id of subcollections, but how can we get their data?
I have tried for listCollections() to get the id of sub-collections, but have not found a way to get data sub-collections data.
Is it possible to get data of sub-documents by reading only the main document in Firebase?
In Firestore, the queries are shallow. This means that it can only return documents from the collection that the query is run against. So there is no way you can get documents along with the data that exists inside sub-collections in one go. A single query can only read fields of documents in a single collection.
If you need to get documents from sub-collection, you need to perform separate queries. If you have collections or sub-collections that have the exact same name then you can use a collection group query.
Firebase listCollectionIds(),Firebase listDocuments() are well documented by firebase but nobody use and write on it....
I think the best way is to list subcollection to your document throught the well-know get() and then process a foreach on result snapshot
snapshot.forEach(doc =>{
console.log(doc.id)
console.log(doc.data())
})
How does one filter a Firestore collection by a document reference in the web console? For example, I have a collection that references another collection document called challenges.
But no matter what combination I use to try to filter that collection by the challenge ID it returns no results. I need to be able to do this so I can look at all of the documents in this collection that reference back to that particular challenge reference.
I am assuming this is possible in the console, but maybe it is not?
I'm using Firebase REST API to get documents. Now the following url works fine:
https://firestore.googleapis.com/v1beta1/projects/{project}/databases/(default)/documents/{collection}/{doc_id}
However I'd like to know if it's possible to get a document only with {doc_id} without specifying the collection?
Thanks so much!
With the Firestore REST API, you will not be able to query the database for all documents with a specific ID under all possible collections. This will not be possible with the other SDKs as well.
What you could do with the REST API is listing all the (sub-)collections of a given document, see https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/listCollectionIds?authuser=0. You will receive collectionIds array of Collections IDs and you will be able to iterate on this array to search for all the documents with a specific ID within those collections.
Note that it doesn't seem to be possible to list the top-level collections for the database.
I'm using Firebase Cloud Firestore and can't figure out how to access documents without knowing the specific path. The database structure is users/{user id}/favourites/{favourite id}. There are no fields in users/{user id} only subcollections. Knowing the user id, i can get the favourites for the user, but I can't get a list of users to get everyone's favourites. Here is the code I am trying (Java admin SDK):
db.collection("users").get().get()
which results in an empty Iterable with no DocumentSnapshots.
How can I get a list of the most popular favourites?
EDIT: I've discovered I can get a list of users with no fields if I add a field. Even if I delete it later, it still appears as a document in the collection.
EDIT2: I've discovered that I can create an empty document, so I'm just doing that for now. As a one-off, I can get a list of all users from firebase auth and look up which ones have a favourites collection and just set those to empty documents.
Regarding the actual limitations in querying data based on subcollections values, what is the suggested way to manage multitenancy in Firestore?
I would like to be able to retrieve and limit access to data related to entities/companies the user is part of.
Example data structure :
/companies/{companyId}/users/
/companies/{companyId}/users/{user}/roles
/companies/{companyId}/docs/
Can /companies/{companyId}/users/ be a collection?
How can I only retrieve companies where user own a role in /companies/{companyId}/users ?
Firestore paths alternate from collection to document and back again:
/collection/document/subcollection/subdocument
So yes, in this case, you would have collections of companies, users, and docs. Collections are also implicit in that they are created automatically when documents exist in them, and removed when no documents exist in them.
At present, subcollection queries (e.g. "all users in a given company") aren't supported, so you'll have to structure your query the other way around: having a users collection with company as a property, when performing a query to find all users in that company.
ref.collection('users').where('company', '==', 'ACME').get().then((document) => {/* Do stuff here */});