Does Firestore TTL delete documents in subcollections? - firebase

I know that in Firestore deleting a document doesn't delete its sub-collections.
Does this hold true for documents deleted by the newly added TTL policies? The documentation doesn't state either way.

The documentation does explicitly state that this policy is defined for collection groups. A collection group refers to all of the documents in collections with the same name. Subcollections with different names do not participate in a collection group. So you can be sure, based on the documentation, that a TTL configuration does not apply to subcollections with different names than the collection group where you establish the policy.

Related

Firebase Firestore TTL Policy for nested collections

Is there any way to create a TTL Policy (In Preview) for a nested collection?
The TTL policy works by querying on collection groups, which means you have to specify posts as the collection name and it them will clean up the documents in all posts collections regardless of where they exist in the database.
If you have posts collections in multiple paths and don't want to expire content from all of them, consider giving them more unique names, such as profile_posts for the ones under profiles. Alternatively, use a field that is unique for the TTL that you want to enforce, which is only present in documents you actually want to get auto-deleted.

Restrict specific object key values with authentication in Firestore

I have an object stored in the Firestore database. Among other keys, it has a userId of the user who created it. I now want to store an email address, which is a sensitive piece of info, in the object. However, I only want this email address to be retrieved by the logged in user whose userId is equal to the userId of the object. Is it possible to restrict this using Firebase rules? Or will I need to store that email address in a /private collection under the Firebase object, apply restrictive firebase rules, and then retrieve it using my server?
TL;DR: Firestore document reads are all or nothing. Meaning, you can't retrieve a partial object from Firestore. So there is no feature at rule level that will give you granularity to restrict access to a specific field. Best approach is to create a subcollection with the sensitive fields and apply rules to it.
Taken from the documentation:
Reads in Cloud Firestore are performed at the document level. You either retrieve the full document, or you retrieve nothing. There is no way to retrieve a partial document. It is impossible using security rules alone to prevent users from reading specific fields within a document.
We solved this in two very similar approaches:
As you suggested, you can move your fields to a /private collection and apply rules there. However, this approach caused some issues for us because the /private collection is completely dettached from the original doc. Solving references implied multiple queries and extra calls to FS.
The second option -which is what the Documentation suggests also, and IMHO a bit better- is to use a subcollection. Which is pretty much the same as a collection but it keeps a hierarchical relationship with the parent coll.
From the same docs:
If there are certain fields within a document that you want to keep hidden from some users, the best way would be to put them in a separate document. For instance, you might consider creating a document in a private subcollection
NOTE:
Those Docs also include a good step-by-step on how to create this kind of structure on FS, how to apply rules to them, and how to consume the collections in various languages

Firebase Firestore Read Count?

Firebase Firestore costs based on number of read operations. If I download a higher level document that has more then one sub-documents ( like Downloading a parent node that has more than one child node in Firebase Realtime Database.) then will it be considered as a single read or multiple read? I have not found any point about this in the documentation. Please explain?
Firestore queries are always shallow, and do not consider documents in subcollection. The only way to query for documents in subcollecitons is to target that subcollection with a separate query.
It does not work like Realitme Database, which gives you all child nodes with a parent node.

Best way to convert this firebase real time database structure to firestore data structure?

I want to convert this firebase real time database structure to firestore data structure please do some help.
I want that structure like Posts(collection)/pin(collection)/pid(document)/then the post description , but i know that a collection can't contain another collection so how should i do?
All_Posts node contain pid and pin only to share that post and then get the post details using the pin and pid.
One more thing in my structure Posts-->734...(pin)-->pid-->then post details because i want to retrieve all the pids and the details under a pin .So should i do in this way or like Posts-->pids(which contain pin number)--> then fetch the details. Which one i should do?
Cloud Firestore Data model
Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections.
Each document contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents.
All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.
Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.
Access the link to have more information about Cloud Firestore Data model
Your DB structure
In regards to your case scenario, you can have collections within other collections , these are called subcollections, as the example for a chat app shows here:
You can access these subcollections with the same collection ID by using Collection Group Queries.
Moving Data from Firebase Realtime Database to Cloud Firestore
For the sake of keeping this answer brief, check this link if you are planning on Moving data from Firebase Realtime Database to Cloud Firestore to consult best practices and recommendations.

Cloud Firestore wildcard for indexing

What is the syntax for a wildcard level in Cloud Firestore when setting up indexing? I know there is one for security rules.
Thanks.
Cloud Firestore indexes are based on collection names, not full collection paths. So if you want to create indexes on users/{id}/messages, the correct way to do this is to create an index on messages.
All same-named collections, even if nested under documents, share the same indexes.

Resources