Kora69
Kora69

Reputation: 31

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?

Upvotes: 1

Views: 334

Answers (1)

sllopis
sllopis

Reputation: 2368

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:

enter image description 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.

Upvotes: 1

Related Questions