C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Data Organization in FirestoreCloud Firestore is a NoSQL, document-oriented database. There are no tables or rows, and data is stored in the documents, which are organized into collections. Each document contains a set of key-value pairs to identify a document. These key-value pairs are optimized for storing a large collection of small documents. All the documents must be stored in collections. Documents can contain sub-collections and nested objects, which can include primitive fields like string or complex objects like lists. A document is a light weighted record that contains a field, which maps to values. A name identifies each document, and we can treat documents as lightweight JSON records. srastogi first: "Shubham" last: "Rastogi" born: 1997 Complex, nested objects in a document are known as maps. srastogi name: first: "Shubham" last: "Rastogi" born: 1997 CollectionsDocuments reside in collections, which are very simple containers for documents. Collections are schema-less. We have freedom over the fields which we put in each document and data types that we store in those fields. Documents that are in the same collection can contain and store different fields and different types of data in those fields.
//Colection users //Document 1 srastogi first : "Shubham" last : "Rastogi" born : 1997 //Document 2 arai first : "Arpita" last : "Rai" born : 1997 Sub-collectionsA sub-collection is a collection associated with a specific document. We can create a sub-collection called messages for every room document in our rooms collection. //Coleection Rooms //Document 1 roomA name: "my chat room" messages //Sub-collection 1 message1 from : "Shubham" msg : "www.TheDeveloperBlog.com" //Sub-collection 2 message2 ... //Document 2 roomB ... When we structure our data in Cloud Firestore, we have the following options
We can nest complex objects like arrays or maps within documents. It is easy to set up and streamline our data structure if we have a simple, fixed list of data. The document grows, with larger or growing lists that can lead to slower document retrieval times. So it is not scalable as another option. We can create collections within the document when we have data that might expand over time. As our list grows, the size of the parent document doesn't change and also get full query capabilities on sub-collections. There is one drawback or limitation of sub-collections, i.e., we cannot easily delete sub-collections. Example//Collection Science //Document software name : "software chat" //Sub-collection users //Document srastogi first: "Shubham" last : "Rastogi" //Document prastogi first: "Pearl" last : "Rastogi" //Document optics physics ... Root-level collectionsIn the root-level collection, we create collections at the root level of our database to organize disparate dataset. Example //Collection users //Document srastogi first : "Shubham" last : "Rastogi" born : 1997 //Document prastogi first : "Pearl" last : "Rastogi" born : 1997 //Another root level collection rooms //Document software //Sub-collection messages //Document message1 from : "srastogi" content : "..." //Document message2 from : "srastogi" content : "..."
val srastogiDocumentRef=db.collection("users").document("srastogi")
val usersCollectionRef=db.collection("users")
val srastogiDocumentRef=db.document("users/srastogi") ExampleCreating a reference to a message in the sub-collection val messageRef=dp.collection("rooms").document("roomA").collection("messages").document("message1")
Next TopicRead and Write in Firestore
|