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.
Related
I am a beginner and my firestore database has a collection which has documents and each document has a subcollection without any other fields. I can't find a way to access the ID of the empty document and get the subcollection associated with the doc using the SDK. I could do it on console but I have to too much to do and cannot also make changes to my application for now so I need to use the SDK. Please suggest me some way to access them like the console.
If you know how the CollectionReference of the sub-collection, you can easily get the parent DocumentReference with parent property of the CollectionReference. From the DocumentReference you just need to use the id property.
You didn't share much details on your data model, so it is difficult to give more guidance on how to find the CollectionReference. Mark's answer presents some possible approaches.
So, I used collection group query(collectionGroup) to query the documents in the subcollections(which have the same ids) and found the IDs of the parent document by the parent property.
Huge thanks to #mark carlton and #Renaud Tarnec for these suggestions.
CASE 1: If the subcollections are queried across multiple documents, you could use a collection group query which queries collections of the same name regardless of their position in the hierarchy. This way you don't actually need to know the ID of the document
CASE 2: Use a document ID you will remember for such a case as opposed to having a UID for the ID. This was your queries make much more sense to you
I'm trying to clean up my Google Cloud Firestore database, and I have some subcollections with no parent doc (the parent was deleted). How can I find all of those, using the Firebase Admin SDK, so I can delete them?
You will end up writing a lot of code for this. I'm going to link to nodejs APIs.
For each collection where there could be missing documents, you will need to need to query that collection with listDocuments(). That will return a list of all documents in the collection, including the missing documents that have subcollections. You will then need to iterate the DocumentReferences returned in that list, and call get() on every one of them. The returned DocumentSnapshot will then tell you if it exists or not using its exists property.
After you have all the DocumentReference objects referring to missing documents, you can then follow the instructions in this other question that describes how to delete all nested subcollections under that DocumentReference, go straight to the Firebase documentation.
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.
The article about Best practices for Cloud Firestore states that we should keep the rate of write operations for an individual collection under 1,000 operations/second.
But at the same time, the Firebase team says in Choose a data structure that root-level collections "offer the most flexibility and scalability".
What if I have a root-level collection (e.g. "messages") which expects to have more than 1,000 write operations/second?
If you think at that limitation of 1,000 operations/second it's pretty much but if you find your self in a situation in which you need more than that, then you should consider changing your database schema to allow writes on multiple collections. So you should multiply the number of collections. Having a single collection of messages, in which every user can add messages doesn't sound as a good way to go since you can reach that limitation very soon. In this case you should split that collection into multiple other collections. A possible schema might be the one I have explained in the following video:
https://www.youtube.com/watch?v=u3KwKQddPoo
See, at the end of that video, there is collection named messages which in term contains a roomId document. This document contains a subcollection named roomMessages which contains as documents all messages from a chat room. In this case, there are no chances you can reach that limitation.
But at the same time, the Firebase team says in Choose a data structure that root-level collections "offer the most flexibility and scalability".
But also rememeber, Firestore can as quickly look up a collection at level 1 as it can at level 100, so you don't need to worry about that.
The limit of 1,000 ops/sec per collection only apply to realtime update, so as long as you don't have a snapshot listener this should be okay.
I asked the question on the Cloud Firestore Google Groups
The limit is 10,000 writes per second if no other limits apply first:
https://firebase.google.com/docs/firestore/quotas#writes_and_transactions
Also just keep in mind the best practices for scaling cloud firestore
I am trying to make feed/timeline where a user can follow - Category,Album or another User. Every time a picture is added to Category,Album,User it should appear on the timeline. I am trying to model my database so it requires 1-2 get requests only.
One idea for the solution is fan-out structure, But how do i make the multi-path update in Firestore? How can i update all the followers timelines when someone uploads a photo ?
How do i structure the database when i cant query on sub-collections? Should i just make one collection which contains all user timeline posts as separate documents, which will be ridiculous amount of duplicated data.
Is there any other way instead of fan-out to structure a user timeline ?
But how do I make the multi-path update in Firestore?
The equivalent of Firebase Realtime Database's multi-path updates, are called batched writes in Cloud Firestore. You can read more in the documentation on batches writes.
Flat.
root
pictures
uid-abc123
url:"http://test.com/img1.jpg"
owner:useriduid,
created: 1529333679449
uid-abc1billion
url:"http://test.com/img1billion.jpg"
owner:useriduid,
created: 1529333679300
Querying and security rules are then easy as pie. You can add indexing and it's very scalable.
Re multipath writes, use batched writes.