英文:
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.
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());
}
});
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论