Firestore创建集合但不添加任何文档。

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

Firestore create collection without adding any document

问题

我想创建一个集合,但不创建其中任何文档。但不是先创建文档,然后再删除文档。我尝试了一些方法,但是当我删除文档时,我的集合也被删除了。

FirebaseFirestore db = FirebaseFirestore.getInstance();

DocumentReference roomReference = db.collection("messagingRooms").document();

List<String> partners = new ArrayList<>();
partners.add("my_id");
partners.add("it's id");

Map<String, Object> newRoom = new HashMap<>();
newRoom.put("partners", partners);
newRoom.put("roomID", roomReference.getId());

roomReference.set(newRoom)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {

                Map<String, Object> newMessage = new HashMap<>();

                DocumentReference messageReference = roomReference.collection("messages").document();

                messageReference.set(newMessage)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {

                                db.collection("messagingRooms")
                                        .document(roomReference.getId())
                                        .collection("messages")
                                        .document(messageReference.getId())
                                        .delete()
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                Log.v("Mok", messageReference.getId());
                                            }
                                        });
                            }
                        });

            }
        }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.v("Error_1", e.getMessage());
    }
});
英文:

I want to create a collection without creating any document in it. But not by first, creating the document, and then deleting the doc. I tried something, but still when I delete the document, my collection is being deleted too.

Firestore image

FirebaseFirestore db = FirebaseFirestore.getInstance();

        DocumentReference roomReference = db.collection(&quot;messagingRooms&quot;).document();

        List&lt;String&gt; partners = new ArrayList&lt;&gt;();
        partners.add(&quot;my_id&quot;);
        partners.add(&quot;it&#39;s id&quot;);


        Map&lt;String ,Object&gt; newRoom = new HashMap&lt;&gt;();
        newRoom.put(&quot;partners&quot;, partners);
        newRoom.put(&quot;roomID&quot;, roomReference.getId());

        roomReference.set(newRoom)
                .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                    @Override
                    public void onSuccess(Void aVoid) {

                        Map&lt;String, Object&gt; newMessage  = new HashMap&lt;&gt;();

                       DocumentReference messageReference =  roomReference.collection(&quot;messages&quot;).document();

                        messageReference.set(newMessage)
                               .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                                   @Override
                                   public void onSuccess(Void aVoid) {
                                       
                                      db.collection(&quot;messagingRooms&quot;)
                                              .document(roomReference.getId())
                                              .collection(&quot;messages&quot;)
                                              .document(messageReference.getId())
                                              .delete()
                                              .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                                                  @Override
                                                  public void onComplete(@NonNull Task&lt;Void&gt; task) {
                                                      Log.v(&quot;Mok&quot;, messageReference.getId());
                                                  }
                                              });
                                   }
                               });

                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.v(&quot;Error_1&quot;, e.getMessage());
            }
        });

答案1

得分: 3

Firestore中没有办法创建一个没有任何文档的集合。如果您查阅官方文档,最后一段提到:
>您不需要“创建”或“删除”集合。在您在集合中创建第一个文档之后,该集合就存在了。如果您删除了集合中的所有文档,它将不再存在。

因此,集合中至少需要有一个文档。

英文:

There is no way of creating a collection without any document in firestore. If you check the official documentation, the last paragraph says:
>You do not need to "create" or "delete" collections. After you create the first document in a collection, the collection exists. If you delete all of the documents in a collection, it no longer exists.

So, a collection needs at least one document in it.

huangapple
  • 本文由 发表于 2020年8月17日 20:48:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63451158.html
匿名

发表评论

匿名网友

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

确定