This question already has answers here:
How to list subcollections in a Cloud Firestore document
(4 answers)
Closed 5 years ago.
I am reading the docs and see that FireStore allows sub-collection. Which is great. Consider the following example as mentioned here int he docs.
As shown in the docs I can get the reference to the last doc as follows
var messageRef = db.collection('rooms').doc('roomA')
.collection('messages').doc('message1');
In the above example, the id's for docs and collections are typed in.
There are cases when id's are dynamically generated. In such a case how can I know how many collections a doc has? Or whether a doc has any sub-collections. How can I do that?
On mobile clients, you can't know how many collections or what collections a document has. There is just no API for that. You have to know ahead of time what collections may exist for a document, or accept that a query on an unknown subcollection may return no documents.
Related
This question already has answers here:
What are the benefits of using a root collection in Firestore vs. a subcollection?
(2 answers)
Closed 7 months ago.
I am working on a quiz game, where players have to answer questions of different categories (sports, music, .....). Right now, I have two different ideas how to store these categories /questions in Firestore:
Creating a new Root-level collection for each category. Every document in this collection would define a question of that category. Stcuture would look like this:
user_collection
user_1_document
user_2_document
user_X_document
music_questions_collection
music_questions_1_document
music_questions_2_document
music_questions_X_document
sports_questions_collection
sports_questions_1_document
sports_questions_2_document
sports_questions_X_document
Creating ONE Root-level collection and create a document inside this collection for each category, where each document contains a subcollection of questions:
user_collection
user_1_document
user_2_document
user_X_document
category_collection
music_document
music_question_subcollection
music_question_document_1
music_question_document_2
music_question_document_3
sports_document
sports_question_subcollection
sports_question_document_1
sports_question_document_2
sports_question_document_3
I have already read about some advantages and limitations of subcollections and root-collections, but have not found enough information regarding read-costs and speed for this specific use case. Can anyone give me some information about drawbacks and advantages of the two listed approaches.
I have not found enough information regarding read-costs and speed for
this specific use case. Can anyone give me some information about
the drawbacks and advantages of the two listed approaches?
In terms of "read-costs and speed", the two approaches are equivalent: using a root collection or a sub-collection is exactly the same.
As a matter of fact, from a technical perspective, a root collection and one of its sub-collections are not at all related to each other. They just share a part of their path but nothing else.
So, since you store the same documents in either a root collection or a sub-collection (i.e. same number of docs in each category collection and same field structure for these docs), there is no difference.
This question already has answers here:
Firestore - get specific fields from document
(4 answers)
How to access a specific field from Cloud FireStore Firebase in Swift
(5 answers)
How to get a list of document IDs in a collection Cloud Firestore?
(4 answers)
Closed 1 year ago.
Suppose I wanted to use Google Firebase's Firestore as the backend of a simple website showing a list of (journal) entries, and the ability to view an entry, edit an entry, and delete an entry. Fundamentally, I would want to retrieve the list of entry titles, so I could present in my website a table of contents. Then when the visitor clicks one of the titles, the website would navigate to the entry's content.
Now, my question is, how do I get just the titles without the content, of the entries? As far as I have read, this is not possible. I present this problem here to confirm whether I have missed something, or if it is indeed impossible with Firebase to get some of the data from a collection of records, without having to retrieve all of the data.
how do I get just the titles without the content, of the entries?
As you've already found, the client-side SDKs for Firestore always retrieve full documents. The server-side SDKs and REST API have a projection/selection mechanism to retrieve only a subset of the fields, but that ability does not exist in the client-side SDKs.
This question already has answers here:
How to list subcollections in a Cloud Firestore document
(4 answers)
Closed 2 years ago.
I am looking towards listing all the collections under doc of firestore. Is there any way to achieve this?
listCollections ad other stuffs are not working.
It is not possible, with the Client SDKs to list the sub collections of a Firestore document (nor the root collections of the Firestore database).
On the other hand, this is possible with the Admin SDKs. You may be interested by this article which shows how to use the listCollections() method of the Admin Node.js SDK in a Cloud Function in order to get the sub collections of a Firestore document.
This question already has answers here:
How do you force a Firestore client app to maintain a correct document count for a collection?
(2 answers)
Closed 3 years ago.
I need to limit the number of documents a user can have in a collection.
I expect that having a limit of let's say 100 documents when a user tries to create the document 101 gets an error.
Is there a way of doing this using firestore security rules ?
Security rules don't have the capability to count the number of documents in a collection. In fact, counting documents in Firestore is, in general, is kind of a difficult problem that typically requires some support from a product like Cloud Functions.
If you want to get something like this to work, you will have to write some Firestore triggers in Cloud Functions that manages the count of documents by triggering some code when a document is created or deleted. This count would have to be stored in another document in another collection. Then, the contents of that document could be used in security rules to limit client access.
This question already has answers here:
Firestore query documents with only collections inside
(2 answers)
Closed 5 years ago.
Is there any naming convention for document id in FireStore. I created some documents inside a collection with the date in "yyyy-mm-dd" as the id. When i try to get all documents inside the collection i got the snapshot size as zero. When I put a dummy doc with test as id I got only the test doc not the other docs.
My document structure
The contraints can be seen here Quotas and Limits if you scroll down to the bottom you will find contraints on ids, paths and field names.