uber
uber

Reputation: 5073

How to delete part of a nested collection in Firebase?

I have not succeeded in deleting part of a nested collection in Firebase. Here's a photo of the collection:

nested collection

And this is what I am trying to do, to delete, for e.g., 0A7vtcos... inside of offeredServices

async function deleteOfferedServiceFromDatabase() {
   const businessRef = await db.collection("businessesPendingAdminApproval")
      .doc(businessId)
      .collection('offeredServices')
      .doc(props.id) //props.id is equivalent to '0A7vtcos...'
      .get()

   businessRef.delete() //won't work 
}

deleteOfferedServiceFromDatabase()

Upvotes: 0

Views: 176

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Your document doesn't actually have any nested subcollections. It has a nested field in a document field called "offeredServices".

If you want to remove a nested field, you will have to update the document by specifying the nested field name (using field value dot notation), and telling Firestore to remove the field using FieldValue.delete():

async function deleteOfferedServiceFromDatabase() {
  const businessRef = db
    .collection("businessesPendingAdminApproval")
    .doc(businessId)

   await businessRef.update(`offeredServices.${props.id}`, FieldValue.delete())
}

Upvotes: 2

Related Questions