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

查看:101
本文介绍了如何构造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?

推荐答案

有此问题分为两个部分:

There are two parts to this problem:


  1. 计算邮件数

  2. 跟踪用户阅读的内容



计算邮件数量



假设您正在使用Firebase Realtime数据库或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天全站免登陆