英文:
Deleting a document from firestore without name of collection
问题
我有一个安卓应用程序,用户可以在文件夹中存储PDF文件。我正在使用Firebase Storage来存储PDF文件,使用Firestore来存储URL和它们的名称。我在删除Firestore数据方面遇到了问题。
目前,这是我的数据结构:
FolderName
|
|---PDFname--URL
--Name
基本上,FolderName是集合。所以我有多个集合。在每个集合/文件夹中,有多个PDF文件。特定文件夹的多个PDF详细信息存储为文档。每个文档都有字段:URL,名称。
现在我希望用户能够删除特定文件夹中的特定PDF。当他点击文件夹时,他可以看到所有的PDF文件。他长按一个PDF文件以删除它。但现在我不知道它属于哪个集合/文件夹。如果我知道它的uid但不知道集合是否可以删除文档吗?
还是有更好的方式来组织我的数据?
编辑:这是我的Firestore数据的截图-
1、2、3、4等是文件夹。文件夹3包含我想要删除的PDF。这个文件夹/集合可能包含多个PDF。
英文:
I have an android app where users can store pdfs in folders. I'm using Firebase Storage for storing PDFs and Firestore for storing the urls and their names. I'm facing a problem in deleting the Firestore data.
Currently here's my data structure:
FolderName
|
|---PDFname--URL
--Name
Basically FolderName is the collection. So I'm having multiple collections. In each collection/folder there are multiple PDFs. The multiple PDF details of a particular folder are stored as documents. Each document has fields : URL,name.
Now I want the user to be able to delete a particular PDF of a particular Folder. When he has clicked on a folder, he can see all the PDFs. He long presses a PDF to delete it. But now I don't know the collection/folder to which it belongs. Is it possible to delete a document if I know its uid but not the collection?
Or is there a better way to structure my data?
Edit: Here is a screenshot of my firestore data-
1,2,3,4 etc are the folders. Folder 3 is containing a PDF which I want to delete. This folder/collection may contain more than one PDF.
答案1
得分: 1
根据Cloud Firestore中删除操作的官方文档,删除文档的方法如下:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("3");
DocumentReference pdfRef = collRef.document("Resources.pdf");
pdfRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("TAG", "Resources.pdf 删除成功!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("TAG", "删除文档时出错", e);
}
});
这段代码将永久删除集合3
中的Resources.pdf
文档。如果需要删除同一集合中的另一个文档,只需将上面的pdfRef
更改为:
DocumentReference pdfRef = collRef.document("AnotherResources.pdf");
如果要从另一个集合中删除PDF,例如从集合2
中删除,只需使用:
CollectionReference collRef = rootRef.collection("2");
DocumentReference pdfRef = collRef.document("ResourcesName.pdf");
英文:
According to the official documentation regaring the delete operation in Cloud Firestore, the way you can remove a document is as follows:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("3");
DocumentReference pdfRef = collRef.document("Resources.pdf");
pdfRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("TAG", "Resources.pdf successfully deleted!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("TAG", "Error deleting document", e);
}
});
This code will permanently remove the Resources.pdf
document from collection 3
. If you need to delete another document within the same collection, you should change the pdfRef
above to:
DocumentReference pdfRef = collRef.document("AnotherResources.pdf");
And if you want to delete a PDF from another collection, for example from 2
collection, simply use:
CollectionReference collRef = rootRef.collection("2");
DocumentReference pdfRef = collRef.document("ResourcesName.pdf");
答案2
得分: 1
如果我知道一个文档的 uid 但不知道集合名称,能否删除该文档?
不可以,这是不可能的。您必须能够构建完整的文档路径才能删除它。
不建议使用动态数据来命名集合。集合的名称应该是静态的,并且始终由您的代码明确知晓,以便您可以使用它们执行查询操作。
相反,考虑将所有动态数据放入文档中,这样可以通过对静态命名的集合进行查询来找到这些文档。我不清楚确切应该怎么做,但您绝对应该创建一种方法,以根据您可用的输入来查找文档。通常,这涉及在文档中创建属性,您可以使用这些属性进行查询。
还请注意,Firestore 的组织方式与计算机文件系统不同。集合与文件夹完全不同。它们只是文档的容器,您应该设计数据以便可以使用集合来查询文档。
英文:
> Is it possible to delete a document if I know its uid but not the collection?
No, it's not possible. You must be able to construct a full path to a document in order to delete it.
It's not a good idea to name collections with dynamic data. The names of collections should be static, and always well-known by your code, so that you can use them to perform queries.
Consider instead putting all dynamic data in documents, so they can be found by queries on your statically named collections. It's not clear to me what exactly you should do instead, but you should definitely create for yourself a way to find documents based on the inputs you have available. This normally involves creating properties in your documents that you can use to query.
Note also that Firestore is not organized like a computer filesystem. Collections are not at all the same as folders. They are just containers for documents, and you should design your data so that you can query to find documents using collections.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论