删除 Firebase 中带有子集合的集合

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

delete collection with subcollections in firebase

问题

我在我的Firestore中创建了以下结构:

/Users/USERID/Chats/UNKNOWN_DOCUMENT_ID/isClicked/isClicked-> 字段:isClicked = 0/1

即:

/collection/document/collection/document/collection/document/字段

现在,我想为用户添加一个选项,以便删除他们的账户,这也意味着要删除上面的路径。然而,我无法弄清楚如何删除它。

我尝试了以下操作:

db.collection("Users").document(auth.getUid()).collection("Chats").get()
        .addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if (document != null) {
                        db.collection("Users").document(auth.getUid()).collection("Chats").document(document.getId()).collection("isClicked").document("isClicked").delete();
                    }
                }
            }
        });

但是我始终得到task.getResult的大小为0,并且没有文档,即使肯定存在文档,但我不知道它的ID。

有没有解决办法可以删除它?

我看到不推荐在代码内部删除集合,但还是有解决方法的。

谢谢

英文:

I have created the following structure in my firestore:

/Users/USERID/Chats/UNKNOWN_DOCUMENT_ID/isClicked/isClicked-> Field: isClicked = 0/1

Which is:

/collection/document/collection/document/colection/document/Field

Now, I want to add an option for users to delete their account which also means to delete the path above. However, I cant figure out how to delete it.

I tried to do the following:

db.collection( "Users" ).document(auth.getUid()).collection("Chats").get()
        .addOnCompleteListener( task -> {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if (document != null) {
                        db.collection( "Users" ).document(auth.getUid()).collection("Chats").document(document.getId()).collection("isClicked").document("isClicked").delete();
                    }
                }
            }
        } );

But I keep getting that task.getResult size is 0 and there is no document even though that there is a document for sure but I don't know its ID.

Any solution to how to delete it?

I read that it is not recommended to delete the collection from within the code but still.

Thank you

答案1

得分: 0

你可能实际上并没有在"/Users/USERID/Chats/"下编写任何文档。如果是这种情况,那么你的查询将不会返回任何结果。在文档ID下创建一个子集合不会自动创建该文档。

查看该文档ID的Firestore控制台。它是否是斜体?如果是这样,实际上并没有文档。它只是一个“幻影文档”占位符,可以让你进入子集合。这些文档无法查询,也无法删除。您将不得不删除所有嵌套的子集合,以使其从控制台中删除。

另请参见:

英文:

You probably didn't actually write any documents under "/Users/USERID/Chats/". If that's the case, then your query will return nothing. Creating a subcollection under a document ID will not automatically create that document.

Look at the Firesotre console for that document ID. Is it in italics? If so, there is not actually a document there. It is just a "phantom document" placeholder that lets you click into the subcollection. These documents can't be queried, nor can they be deleted. You would have to delete all of its nested subcollection in order to see it removed from the console.

See also:

huangapple
  • 本文由 发表于 2020年10月22日 04:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64470928.html
匿名

发表评论

匿名网友

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

确定