简体   繁体   English

如何在 queryDocumentSnapshot Firestore 中查询文档字段

[英]How to query a document field inside of queryDocumentSnapshot Firestore

I am building a chatroom application and am trying to query all messages then separate them accordingly based on the message sender.我正在构建一个聊天室应用程序,并尝试查询所有消息,然后根据消息发件人将它们相应地分开。

This is what my Firestore architecture looks like:这是我的 Firestore 架构的样子:

在此处输入图片说明

And my code so far:到目前为止我的代码:

 CollectionReference chatRoomMsgs = db.collection("chatrooms").document(chatRoomID).collection("Messages");
    chatRoomMsgs.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){
                        if(documentSnapshot.get("sentby") == firebaseUser.getUid()){

                        }
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });

What I am (currently) trying to do is pull ALL chatroom messages first, and then separate them out in onSuccess.我(目前)尝试做的是首先拉取所有聊天室消息,然后在 onSuccess 中将它们分开。

I am trying to say "ok if the message was sent by this user, grab the image field value of that same document and add it to an array so the image can be accessed later, and if the message was not sent by the same user, also grab the image url but add it to a different array"我想说“好的,如果消息是由该用户发送的,则获取同一文档的图像字段值并将其添加到数组中,以便稍后可以访问图像,如果消息不是由同一用户发送的, 也获取图像 url 但将其添加到不同的数组中"

How can I do this?我怎样才能做到这一点? Thanks!谢谢!

Update I tried adding the while loop below to get some sort of output, wasn't triggering更新我尝试添加下面的while循环以获得某种输出,没有触发

ArrayList<String> sentPics = new ArrayList<String>();      
while(documentSnapshot.get("sentby") == firebaseUser.getUid()){
                                    sentPics.add(documentSnapshot.get("image").toString());
                                    Log.d("PICLIST", sentPics.toString());
                                }

If you want to get all the messages sent by a specific user, then you should use the following query:如果要获取特定用户发送的所有消息,则应使用以下查询:

CollectionReference chatRoomMsgs = db.collection("chatrooms").document(chatRoomID).collection("Messages");
Query sendByQuery = chatRoomMsgs.whereEqualTo("sentby", firebaseUser.getUid());
sendByQuery.addOnSuccessListener(/* ... */);

Using this solution you'll significantly reduce the number of read operations as you get as a result only the messages that correspond to the logged-in user.使用此解决方案,您将显着减少读取操作的数量,因为您只会获得与登录用户对应的消息。

Your solution is very expensive because you are getting all messages that exist in the Messages collection and you filter them on the client.您的解决方案非常昂贵,因为您将获取Messages集合中存在的所有消息,并在客户端对其进行过滤。 If you have a total of 100 messages in the collection but only 10 correspond to the logged-in user, you'll be charged with 100 reads.如果您在集合中总共有 100 条消息,但只有 10 条对应于登录用户,则您需要支付100 次阅读费用。 In my solution, you'll only be charged with 10, as the query only 10 messages returns.在我的解决方案中,您只需支付 10 条,因为查询仅返回 10 条消息。

If want to see another approach, here you can find a tutorial on how to create a complete and functional Firestore Chat App .如果想了解另一种方法,您可以在此处找到有关如何创建完整且功能齐全的Firestore 聊天应用程序的教程。

What you need to do is make a POJO named Message that maps to your Messages collection with member variables image and sentby and convert the documentSnapshot to a Message object using:您需要做的是创建一个名为Message的 POJO,它使用成员变量imagesentby映射到您的 Messages 集合,并使用sentbydocumentSnapshot转换为Message对象:

Message message = documentSnapshot.toObject(Message.class)

From there on, you can just use the getters to achieve what you want.从那时起,您可以只使用 getter 来实现您想要的。

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM