Firestore数据存储在子集合中 [英] firestore data storing in sub collections

查看:179
本文介绍了Firestore数据存储在子集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FirebaseFirestore.getInstance在Firestore中进行聊天演示以保存消息()
.collection(Consts.R_CHAT_ROOM)
.document(finalChatRoom)
.collection(messages)
.document(currentTime)
.set(chatModel );

但问题在于 finalChatRoom 显示它不存在,虽然它包含一个子集。





在这里写了:这个文档不存在,虽然它包含一个名为 messages 的子集合,它包含更多的文档。



但是我需要检查具有特定名称的文档是否存在于 chatRoomsMessages 集合中。



有没有我的代码有问题,或者我需要以其他方式做?



提前致谢

解决方案

在不存在的文档中创建子集合非常类似于使用子集合创建文档,然后删除文档。从数据模型文档的子集合部分,这意味着: p>


删除具有关联子集合的文档时,子集合不会被删除。他们仍然可以参考。例如,可能有一个由 db.collection('coll')。doc('doc')。collection('subcoll')。doc('subdoc') db.collection('coll')。doc('doc')引用的文档不再存在。


blockquote>

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

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

//创建聊天室文档
ChatRoom chatRoomModel = new ChatRoom(聊天室1);
chatRoomDocument.set(chatRoomModel);

//用新文档创建消息子集合
chatRoomDocument.collection(messages)
.document(currentTime).set(chatModel);

其中 ChatRoom 类是类似于:

  public class ChatRoom {
private String name;

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

public String getName(){
return name;
}

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

// ...
}



<这是利用Firestore的自定义对象功能。如果您不想在此阶段使用自定义对象,则可以创建一个简单的 Map 来代表聊天室:

 映射< String,Object> chatRoomModel = new HashMap<>(); 
chatRoomModel.put(name,聊天室1);


I am making a "chatting demo" in Firestore for saving messages I am doing it like this:

FirebaseFirestore.getInstance()
    .collection(Consts.R_CHAT_ROOM)
    .document(finalChatRoom)
    .collection("messages")
    .document(currentTime)
    .set(chatModel);

But the problem is that the finalChatRoom document shows that it does not exist although it contains a subcollection.

As it's written there: "this document does not exist" although it contains a sub-collection named messages in it which contains further documents.

But I need to check if the document with a specific name is present under the chatRoomsMessages collection or not.

Is there any problem with my code or do I need to do it in some other way?

Thanks in advance.

解决方案

Creating a subcollection in a document that does not exist is much like creating a document with a subcollection and then deleting the document. From the Subcollections section of the Data Model documentation this means:

When you delete a document that has associated subcollections, the subcollections are not deleted. They are still accessible by reference. For example, there may be a document referenced by db.collection('coll').doc('doc').collection('subcoll').doc('subdoc') even though the document referenced by db.collection('coll').doc('doc') no longer exists.

If you want the document to exist, I'd suggest creating the finalChatRoom document first, with at least one field, and then creating the subcollection underneath it. For example:

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);

Where the ChatRoom class is something like:

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;
    }

    // ...
}

This is making use of the custom objects functionality of Firestore. If you don't want to use a custom object at this stage, you could just create a simple Map instead to represent the chat room:

Map<String, Object> chatRoomModel = new HashMap<>();
chatRoomModel.put("name", "Chat Room 1");

这篇关于Firestore数据存储在子集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆