如何构建 NoSQL 消息以通过 1 个查询获取未读内容? [英] How to structure NoSQL messages to get unreads by 1 query?

查看:24
本文介绍了如何构建 NoSQL 消息以通过 1 个查询获取未读内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的 NoSQL 结构如下:

Say I have NoSQL structure as follows:

messages  
  chat_id (known)  
    message_id (generated automatically)  
      {author, timestamp, content}

我还有 users/ 分支,我可以在其中查看所涉及的两个用户的上次登录.
我想通过 1 个查询获取给定用户的未读消息数.

I also have users/ branch where I can see last logins of both users involved.
I want to get the number of unread messages for a given user by 1 query.

您将如何实施这样的任务?

How would you implement such a task?

推荐答案

这个问题有两个部分:

  1. 计算消息数量
  2. 跟踪用户阅读的内容

统计消息数量

假设您使用 Firebase 实时数据库或 Cloud Firestore,有两种典型的计数方法:

Counting the number of messages

Assuming you are using the Firebase Realtime Database or Cloud Firestore, there are two typical approaches to counting:

  1. 加载未读消息并在客户端对其进行计数.
  2. 在数据库中保留消息计数器,并在每次读/写消息时更新该计数器.

在客户端计算消息是最容易实现的,因此通常是没有经验的开发人员从什么开始.当消息数量很少时,这可以正常工作.但随着消息数量的增加,您将下载越来越多的数据.

Counting the messages client-side is easiest to implement, so typically what inexperienced developers start with. When the number of messages is low, that works fine. But as the number of messages grows, you'll be download more and more data.

此时,您将切换到在数据库中保留一个计数器,并在每次写入/删除操作时更新该计数器.这会复制数据,因此通常是具有关系数据库背景的开发人员难以解决的问题.但这是最常见的方法.

At that point you'll switch to keeping a counter in the database and updating that on every write/delete operation. This duplicates data, so is typically something that developers with a background in relational databases struggle with. But it is the most common approach.

您可以更新计数器客户端,或使用服务器端方法,例如 在 Cloud Functions 中维护计数器的示例.

You can update the counter client-side, or use a server-side approach such as this example of maintaining counters in Cloud Functions.

要知道用户有多少未读消息,您需要知道用户已经阅读了哪些内容.同样,这里有两种常见的方法:

To know how many unread messages a user has, you need to know what the user has already read. Here again, there are two common approaches:

  1. 跟踪用户阅读的每条消息.
  2. 跟踪用户最近阅读的消息的 ID.

跟踪单个消息听起来最正确,所以我看到这已经做了很多.我也看到开发人员为它苦苦挣扎,因为它涉及大量的簿记.这绝对有可能,但我建议从更简单的方法开始......

Tracking the individual messages sounds most correct, so I see this being done a lot. I also see developers struggling with it, since it involves a lot of book keeping. It's definitely possible, but I'd recommend starting with a simpler approach...

聊天应用程序处理(按时间顺序)消息序列.如果您知道用户看到的最新消息的 ID,您可以假设用户还看到了所有早于该消息的消息.

Chat apps deal with (chronological) sequences of messages. If you know the ID of the latest message that the user has seen, you could assume that the user has also seen all messages that are older than that.

假设我跟踪了您阅读的最后一条消息的时间戳,它是 1535725374298.我现在可以查询聊天以仅检索更新的消息:

Say that I track the timestamp of the last message you read, and it's 1535725374298. I can now query the chat to only retrieve messages newer than that:

firebase.database()
  .ref("chat")
  .child(chatId)
  .orderByChild("timestamp")
  .startAt(1535725374298)

这篇关于如何构建 NoSQL 消息以通过 1 个查询获取未读内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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