1

I have a subcollection 'favourites' under my 'users' collection and I would like to delete any of its documents by checking that the field 'prod_id' is isEqualTo prodId

THis is how I create the subcollection & document when a user taps the favourite button:

      _usersCollectionReference
          .doc(userId)
          .collection('favourites')
          .add({'prod_id': prodId});

Now, I want to be able to delete this document

William Chidube
  • 183
  • 3
  • 16

3 Answers3

3

To delete any document you must know it's ID or have a DocumentReference to it. For that you just need to know the userId and fetch documents where prod_id is equal to the product IDs you want to delete.

FirebaseFirestore.instance
  .collection('users')
  .doc(userId)
  .collection('favourites')
  .where('prod_id', whereIn: [...])
  .get()
  .then((snapshot) {
    // ... 
  });

snapshots is a QuerySnapshot and has a property docs which is an array of QueryDocumentSnapshot. You can loop through that array and delete each document by accessing their reference.

For example, deleting first document in that would be:

snapshot.docs[0].reference.delete()
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • [This answer](https://stackoverflow.com/a/68378313/13130697) uses Javascript but should be good to clarify the same. – Dharmaraj Jul 18 '21 at 16:49
0

you can do this very easily and return a result

    let contentArray: any[] = []
  // call the content collection
  const contentRef = db.collection('favourites');
  // snapshot data on get
  const snapshot = await contentRef.get().then((querySnapshot)=>{
// return query snapshot 
      return querySnapshot.docs
      // .map(doc => doc.data());

  })
  // this statement can be joined to the other
  // loop through each snapshot as document
 snapshot.forEach((doc)=>{
   // create new constant as document of each loop
    const newdoc = doc.data()
    // push new constant to content array
    if(newdoc.YOURTHING==='your thing you want to look for'){
    const deleteDocId=newdoc?.id

db.collection('favourites').doc(`${deleteDocId}`).delete().then((result)=>{
              return result
            })
   }

  })
})

You can read about why and how here: https://firebase.google.com/docs/firestore/manage-data/delete-data

chris burd
  • 728
  • 3
  • 9
  • Perhaps I wasn't clear enough. Passing the userId isn't sufficient to know which specific favourite document to delete. To know this, I need to check one of the fields of the favourite document which is where I store the productId and see that it is equal to the prodId received. In other words I (think) have to query the favourite collection/document for the value of a particular field and if that value is equal to the value I received from the onTap, I can then delete that document. – William Chidube Jul 18 '21 at 14:06
  • I understand what you want to do give me a second and I will update the answer above. – chris burd Jul 18 '21 at 14:10
  • Okay thanks. I'll try this out immediately and update you asap. – William Chidube Jul 18 '21 at 14:18
  • this won't be exact William no ones answer will be but it will be very close to what you want - the idea is to loop through each document on a collection and if it matched your variable either let var or const - then it will delete that item. Just be careful if this is a very large collection it can be expensive. you can further refine this by using ref and where on the collection query if you wanted too as well. – chris burd Jul 18 '21 at 14:20
  • This is understood, Chris – William Chidube Jul 18 '21 at 14:33
0

You can also do a check on the collection itself with where logic

const snapshot = await contentRef.where('favouritething','==', favoriteId)
  
  .get().then((querySnapshot)=>{
// return query snapshot 
      return querySnapshot.docs
      // .map(doc => doc.data());

  })
chris burd
  • 728
  • 3
  • 9