Reputation: 35
I am currently trying to make a "delete document" function in my App that is using Firebase's Firestore.
I am using the following code with a hardcoded value:
fun deleteEvent() {
db.collection("events").document("4vom6RfWU9depGXR0Zw0")
.delete()
}
This function deletes the document with the ID "4vom6RfWU9depGXR0Zw0"
What I would want to have instead of the hardcoded value would of course be a method that finds the relevant document ID and places it into a value that I can use instead?
I have scoured the documentation and youtube/google for code that I can use, but most of it seems to be out-of-date or simply not working anymore.
I found several methods that can be used to find both document "Path" and "ID" but these values are not similar to the one that appears in the Firebase UI and cannot be used to delete the document.
I'd appreciate any help and let me know if you need more information!
Upvotes: 1
Views: 1141
Reputation: 138844
What I would want to have instead of the hardcoded value would of course be a method that finds the relevant document ID and places it into a value that I can use instead.
To remove a document by the document ID (4vom6RfWU9depGXR0Zw0), you should add the ID as a property of the document.
Firestore-root
|
--- events (collection)
|
--- 4vom6RfWU9depGXR0Zw0 (document)
|
--- docId: "4vom6RfWU9depGXR0Zw0"
|
--- //The other properties
Now to find that particular document, you need to use a Query that looks like this:
val rootRef: FirebaseFirestore = FirebaseFirestore.getInstance()
val eventsRef: CollectionReference = rootRef.collection("events")
val docIdQuery: Query = eventsRef.whereEqualTo("docId", "4vom6RfWU9depGXR0Zw0")
docIdQuery.get().addOnCompleteListener(object : OnCompleteListener<QuerySnapshot?>() {
fun onComplete(task: Task<QuerySnapshot?>) {
if (task.isSuccessful()) {
for (document in task.getResult()) {
document.getReference().delete().addOnSuccessListener(object : OnSuccessListener<Void?>() {
fun onSuccess(aVoid: Void?) {
Log.d(TAG, "Document successfully deleted!")
}
}).addOnFailureListener(object : OnFailureListener() {
fun onFailure(e: Exception) {
Log.w(TAG, "Error deleting document", e)
}
})
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException()) //Don't ignore potential errors!
}
}
})
And for Java users:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference eventsRef = rootRef.collection("events");
Query docIdQuery = eventsRef.whereEqualTo("docId", "4vom6RfWU9depGXR0Zw0");
docIdQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
document.getReference().delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Document successfully deleted!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error deleting document", e);
}
});
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException()); //Don't ignore potential errors!
}
}
});
This code will, however, work if you for example want to delete a document that has the value of the date
property set to "25 Nov 2020". In this case, the following query should be used:
Query dateQuery = eventsRef.whereEqualTo("date", "25 Nov 2020");
Upvotes: 2