简体   繁体   中英

How to delete a document inside a subcollection

I have a collection STUDENTS which has a sub collection PLAYLIST what I want to do is deleting the documents inside the PLAYLIST

this is the code I have tried

    const result = await deleteDoc(doc(db, path, dataId));

the path is "Students/student_id/Playlist" the dataID is the document i wanted to delete but there are no result when i trigger this action

To delete all the documents inside a sub-collection, you should loop the snapshot and call the deleteDoc() method. See code below:

const querySnapshot = await getDocs(collection(db, "students", "<student_id>", "playlist"));

querySnapshot.forEach((doc) => {
  deleteDoc(doc.ref);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM