Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Best practice for adding a history subcollection upon submitting the form without cloud functions

I have this form to submit the products. Also, I want to store the date of when it was submitted along with the data in the subcollection history. So, this is what I did:

When adding a product and storing the history of it as well

const handleSubmit = async (e) => {
    e.preventDefault();

    const docRef = await addDoc(collection(db, "products"), {
      productName,
      Number(price),
      size,
      cat,
      colorMap,
    });

    const historyRef = await doc(db, "products", docRef.id);
    const colRef = collection(historyRef, "history");
    addDoc(colRef, {
      productName,
      Number(price),
      size,
      cat,
      colorMap,
      createdDate: new Date(),
    });

    setOpen(true);
    console.log("Document written with ID: ", docRef.id);
  };

This is how I save it in Firebase:

enter image description here

I also have another form that in editing a product that will store the changes that we made in another document inside the history subcollection.

The way that I've added the subcollection upon submission, is this alright or would it cause complications in the future? Or should I make a function to call this or are there any other suggestions? Thank you. Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From your question it is clear that your Firestore Database Structure is like the following -

Collection - products

    Document 1
        
        Subcollection - history
            
            History document 1
            History document 2
            ………
    Document 2
        
        Subcollection - history
            
            History document 1
            History document 2
            ………
    Document 3
        
        Subcollection - history
            
            History document 1
            History document 2
            ………

It seems to be fine. But in future, if there are many edits made for a product, then there will be a lot of documents inside the history subcollection, which may seem redundant. So you may consider deleting the documents inside the history subcollection based on your need. For example, you may consider deleting the history which is 6 months old.

You may go through this document to know some best practices while structuring your Firestore database.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!