TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Firebase Data Organization in Firestore

Firebase Data Organization in Firestore with What is Firebase, Features, Android Studio, Assistance, Adding Firebase to App, Installation, Data, Firebase Arrays, Event Types, Firebase Queries, Firebase Security etc.

<< Back to FIREBASE

Data Organization in Firestore

Cloud 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.

Data Organization in Firestore

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

Collections

Documents 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.

  • A collection contains documents and nothing else.
  • It cannot directly contain raw fields with value.
  • It cannot contain other collections.
  • The names of the documents within a collection are unique.
  • We can provide our keys, such as user IDs.
  • We can allow Cloud Firestore to create random IDs for us automatically.
  • There is no need to create or delete collections.
  • The collection exists, when we create the first document.
  • If all the documents in a collection are deleted, the collection will not be longer exists.
//Colection
users
	//Document 1
	srastogi
		first : "Shubham"
		last : "Rastogi"
		born : 1997
	//Document 2
arai
		first : "Arpita"
		last : "Rai"
		born : 1997

Sub-collections

A 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

  • Documents
  • Multiple collections
  • Sub-collections within documents.

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 collections

In 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 : "..."
  • Each document is uniquely identified by its location within the database:
val srastogiDocumentRef=db.collection("users").document("srastogi")
  • We can also create reference to collections
val usersCollectionRef=db.collection("users")
  • We can also create references by specifying the path as a string. The path components are separated by a forward slash(/).
val srastogiDocumentRef=db.document("users/srastogi") 

Example

Creating a reference to a message in the sub-collection

val messageRef=dp.collection("rooms").document("roomA").collection("messages").document("message1")





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf