Android 存储在子集合中的firestore数据

Android 存储在子集合中的firestore数据,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我正在Firestore中制作一个“聊天演示”,用于保存消息,我是这样做的: FirebaseFirestore.getInstance() .collection(Consts.R_CHAT_ROOM) .document(finalChatRoom) .collection("messages") .document(currentTime) .set(chatModel); 但是问题是,finalChatRoom文档显示它不存在,尽管它包含一个子集

我正在Firestore中制作一个“聊天演示”,用于保存消息,我是这样做的:

FirebaseFirestore.getInstance()
    .collection(Consts.R_CHAT_ROOM)
    .document(finalChatRoom)
    .collection("messages")
    .document(currentTime)
    .set(chatModel);
但是问题是,
finalChatRoom
文档显示它不存在,尽管它包含一个子集合

正如上面所写:“此文档不存在”,尽管其中包含名为
messages
的子集合,其中包含更多文档

但是我需要检查带有特定名称的文档是否存在于
聊天室消息
集合下

我的代码有什么问题吗?或者我需要用其他方式来做吗


提前感谢。

在不存在的文档中创建子集合与使用子集合创建文档然后删除文档非常相似。这意味着:

删除具有关联子集合的文档时,不会删除子集合。它们仍然可以通过引用访问。例如,可能存在由
db.collection('coll').doc('doc').collection('subcoll').doc('subdoc')
引用的文档,即使由
db.collection('coll').doc('doc')
引用的文档已不存在

如果您希望文档存在,我建议您首先创建
finalChatRoom
文档,至少包含一个字段,然后在其下方创建子集合。例如:

DocumentReference chatRoomDocument = FirebaseFirestore.getInstance()
        .collection(Consts.R_CHAT_ROOM)
        .document(finalChatRoom);

// Create the chat room document
ChatRoom chatRoomModel = new ChatRoom("Chat Room 1");
chatRoomDocument.set(chatRoomModel);

// Create the messages subcollection with a new document
chatRoomDocument.collection("messages")
        .document(currentTime).set(chatModel);
其中,
聊天室
类类似于:

public class ChatRoom {
    private String name;

    public ChatRoom() {}

    public ChatRoom(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // ...
}
这是在利用。如果在此阶段不想使用自定义对象,您可以创建一个简单的
映射来表示聊天室:

Map<String, Object> chatRoomModel = new HashMap<>();
chatRoomModel.put("name", "Chat Room 1");
Map chatRoomModel=newhashmap();
聊天室模型。输入(“名称”,“聊天室1”);

您的新文档将位于
消息
子集合下。是的,但我需要检查
聊天室消息集合下是否存在具有特定名称的
文档
。感谢您的回复,这对我更好地理解事情非常有帮助,让我试试你告诉我的方式,如果一切顺利,我会把它作为答案。谢谢