32

I have a problem with deleting an Object out of an Array in firestore. I have this data in firestore:

enter image description here enter image description here

And now I would like to delete e.g the second Object out of the posts Array.

Code:

 deletePic () {
  let docId = `${this.currentUser.uid}`

   fb.usersCollection.doc(docId).update({
     posts: firebase.firestore.FieldValue.arrayRemove()
   })
  .catch(function(error) {
      console.error("Error removing document: ", error);
  });
}

But I do not know how to define arrayRemove()

These are the pictures and each one has a delete button to delete the picture.

enter image description here

1

5 Answers 5

58

You could also use the arrayRemove method from the FieldValue helper.

docRef.update({
   array: FieldValue.arrayRemove('idToRemove');
});

https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html

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

2 Comments

This one work only for simple arrays as ["test","test2"] not if u have array of objects [{},{},{}]
@PatrikSpišák instead of saving an array convert is to a map before.
21

EDIT: Warning Do not use my solution as it is more than 3 years old, nowadays there is new solution : https://stackoverflow.com/a/59745086/6668441 Mine was a pure js one and is a bad pattern.

Can't you use filter ? And then return the new posts array to your fb.usersCollection method

//deleteId is the id from the post you want to delete
posts.filter(post => post.id !== deleteId);

edit : So This should be something like :

 deletePic (deleteId) {
  let docId = `${this.currentUser.uid}`

   //deleteId is the id from the post you want to delete

   fb.usersCollection.doc(docId).update({
     posts: posts.filter(post => post.id !== deleteId);
   })
  .catch(function(error) {
      console.error("Error removing document: ", error);
  });
}

8 Comments

and where to set the delete() or arrayRemove()?
This will store a new array without the post you delete, so this is your delete() method
I updated my answer, then you must get the deleteId from the user click delete button
the deleteId should contain inside the object or?
If you are working with a big amount of data, you w'll download all the elements to your app just to delete one. It's not optimized.
|
14

You can preform delete of an object in the array by using arrayRemove function. But, you'll need to provide an object. That object needs to be identical to the one in your doc array on the firestore collection.

For example:

The following code will delete obj from myArray array, but only if obj exactly exist in that array.

const obj = { field1, field2 ... } 

collectionRef.doc(docId).update({
    myArray: firebase.firestore.FieldValue.arrayRemove(obj)
})

Comments

3

Update elements in an array

If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.

// Atomically remove a region from the 'regions' array field.
city_ref.update({u'regions': firestore.ArrayRemove([u'east_coast'])})

Comments

0

If you're using Firebase V9 (modular), you can simply import arrayRemove from Firestore like so:

import { doc, updateDoc, arrayRemove } from 'firebase/firestore';

const docRef = doc(db, 'collection', 'doc');
await updateDoc(docRef, {
  arrayName: arrayRemove('elementName')
}); 

Comments

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.