Shubhang Chourasia
Shubhang Chourasia

Reputation: 153

Which method should I use to store data in firebase firestore document?

I have this sample data of two objects. Both can be put in a document with below two structures. And it's easy to perform CRUD on both methods. But I want to know which is the more efficient one.

Structure 1:

key1:{ sr.:1, name:'Raj', city: 'Mumbai'}
key2:{ sr.:2, name:'Aman', city: 'Delhi'}

It's easy to create different objects inside a single document using merge property and deletion can be performed using the below code.

db.collection('colName')
        .doc('docName')
        .update({
          [key1]: firebase.firestore.FieldValue.delete(),
        })

Structure 2:

It is basically objects in an array.

arr:[ { sr.:1, name:'Raj', city: 'Mumbai'} , 
      { sr.:2, name:'Aman', city: 'Delhi'} ]

The data can be pushed in array arr using the below code.

 ['arr']: firebase.firestore.FieldValue.arrayUnion(object3)

And the deletion can be performed like this.

['arr']: firebase.firestore.FieldValue.arrayRemove(indexToBeDeleted)

Which one is more efficient when it comes to CRUD operations?

Upvotes: 1

Views: 246

Answers (1)

Cadet
Cadet

Reputation: 354

CRUD is 4 different qualities, each of which has additional measurable attributes. Talking about CRUD in the context of firestore adds even more attributes to those as well.

There are firestore limits/quotas: https://cloud.google.com/firestore/quotas And, There are firestores costs: https://firebase.google.com/docs/firestore/pricing

  • firestore charges per read.

Storing all your data into one document is cost efficient.

  • Firestore is optimized for reads.

In the limits/quotas document you may notice that there is a max write rate, to a document, of 1 per second. How frequently would you plan on writing new data into the array of that 1 document? Is 1 document still efficient?

  • Firestore has a max document size of 1MB.

Are you going to write more than 1MB to a document. After adding the logic to split your document apart is it still efficient?

There are many aspects to think about in designing your data structures. An efficiency of one quality is bound to create inefficiencies in another.

Upvotes: 1

Related Questions