从Firestore中删除一个文档,而不需要集合的名称。

huangapple go评论66阅读模式
英文:

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数据的截图-

从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-

从Firestore中删除一个文档,而不需要集合的名称。

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(&quot;3&quot;);
DocumentReference pdfRef = collRef.document(&quot;Resources.pdf&quot;);
pdfRef.delete().addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
    @Override
    public void onSuccess(Void aVoid) {
        Log.d(&quot;TAG&quot;, &quot;Resources.pdf successfully deleted!&quot;);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.w(&quot;TAG&quot;, &quot;Error deleting document&quot;, 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(&quot;AnotherResources.pdf&quot;);

And if you want to delete a PDF from another collection, for example from 2 collection, simply use:

CollectionReference collRef = rootRef.collection(&quot;2&quot;);
DocumentReference pdfRef = collRef.document(&quot;ResourcesName.pdf&quot;);

答案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.

huangapple
  • 本文由 发表于 2020年7月22日 15:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63029234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定