0

I have a Collection called Notes which has a document that is an UID of the user that consists of collection of an Array of objects which look something like this

obj {[
 {title:"Note 1",desc:"description 1"},
 {title:"Note 2",desc:"description 2"},
 {title:"Note 3",desc:"description 3"},
]}

this is the actual Firestore collection where is now allowing me to store objects with the same key using the code I wrote but If I'm trying to add it manually then I

enter image description here

I'm using React with Firebase and using Firestore as a database

the problem I'm facing is that if I add more objects to the array mentioned above with the same key it's not creating a duplicate Firestore is not letting me add more objects with the same key. Other than that it is running just fine.

Below is the code for adding a new Notes Collection

// this function create a new Notes Collection
const createNotes = async (title, description) => {
  const uid = auth.currentUser.uid; // For document name
  const notesRef = doc(db, "Notes", uid);
  const data = {
    note: [{ title: title, description: description }],
  };
    try {
    await setDoc(notesRef, data);
  } catch (error) {
    console.log(error);
  }
};

Below is the code for updating the array of objects, this was the only way I could find to add multiple objects into an array on firestore

const updateNotes = async (title, description) => {
  const uid = auth.currentUser.uid;
  const notesRef = doc(db, "Notes", uid);
  const data = {
    note: arrayUnion({ title: title, description: description }),
  };
  try {
    await updateDoc(notesRef, data, { merge: true });
  } catch (error) {
    console.log(error);
  }
};

Is there a solution to this?

2
  • 4
    I having a really hard time understanding what the problem is? That last code snippet looks like the correct way to add an item to the note array field. What isn't working about it? Commented Oct 10, 2022 at 23:35
  • If suppose I have added an array field such as {title:”a”,desc:”b”} in the array and if again try to add the same object it will not create a duplicate of the same object how will I be able to have multiple same object in an array Commented Oct 11, 2022 at 7:50

1 Answer 1

2

According to your last comment:

If suppose I have added an array field such as {title:”a”,desc:”b”} in the array and if again try to add the same object it will not create a duplicate of the same object how will I be able to have multiple same objects in an array?

Please note that you cannot add duplicate elements in an array using arrayUnion. According to the official documentation:

arrayUnion() adds elements to an array but only elements not already present.

So if you need to have duplicate elements, then the single option that you is to read the document, add the duplicate element in the array, and then write the document back to Firestore.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I know that but if there isn't a way to do it this way then it is really inefficient. And this might be an unpopular opinion but the documentation provided by Firebase for Firestore is not that great anyways thanks man for answering

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.