Mongoose 私聊消息模型 [英] Mongoose Private Chat Message Model

查看:57
本文介绍了Mongoose 私聊消息模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户之间的私人消息添加到我的数据模型中.我一直在两种可能的方式之间来回切换.

I'm trying to add private messaging between users into my data model. I've been going back and forth between two possible ways of doing this.

1) 每个用户都有一组 user_id、chat_id 对,它们对应于他们参与的聊天.聊天模型只存储 chat_id 和消息数组.

1) Each user has an array of user_id, chat_id pairs which correspond to chats they are participating in. Chat model just stores chat_id and array of messages.

2) 根本不存储与用户的聊天记录,只让聊天模型存储一对 user_ids 和消息数组.

2) Don't store chats with user at all and just have the Chat model store a pair of user_ids and array of messages.

选项 (1) 的问题是,每当用户加入或开始聊天时,我都需要先查看用户的数组,以查看 user_id、chat_id 对是否已存在.然后在 Chat 中对 chat_id 进行第二次查找.如果它不存在,我需要在两个不同的地方为参与的两个用户创建 user_id、chat_id 对.

The issue with option (1) is whenever a user joins or starts a chat, I would need to look first through the array for the user to see if the user_id, chat_id pair already exists. And then do a second find for the chat_id in Chat. If it doesn't exist, I would need to create the user_id, chat_id pair in two different places for both users who are participating.

使用选项 (2),我将在 Chat 模型中搜索 user_id1、user_id2 对,如果找到它,我就完成了,如果没有,我将为该对创建一个新的聊天记录并完成.

With option (2) I would search through the Chat model for the user_id1, user_id2 pair, and if I find it I'm done, if not I would create a new Chat record for that pair and done.

基于此选项 (2) 似乎是处理此问题的更好方法.但是,我遇到了一些问题,要弄清楚如何以一种可以在聊天模型中轻松搜索的方式对一对"用户 ID 进行建模.即,即使 user_ids 以错误的顺序传递,即 user_id2、user_id1,我如何确保我能找到聊天记录.在猫鼬中建模的最佳方法是什么?

Based on this option (2) does seem like the better way of handling this. However, I'm running into issues figuring out how to model the "pair" of user ids in a way that they are easily searchable in the chat model. i.e. how do I make sure I can find the chat record even if the user_ids are passed in the wrong order, i.e. user_id2, user_id1. What would be the best way to model this in Mongoose?

var chatSchema = mongoose.Schema({

  messages: [{
        text: { 
          type: String,
          max: 2000
        },
        sender: { 
          type: mongoose.Schema.Types.ObjectId, 
          ref: 'User'
        }
      }],
  participant1: [{                
          type: mongoose.Schema.Types.ObjectId, 
          ref: 'User'
        }]
  participant2: [{                
          type: mongoose.Schema.Types.ObjectId, 
          ref: 'User'
        }]
});

如果是上述情况,我将如何搜索参与者对?我能否以某种方式对参与者 ID 进行排序,以便它们始终是参与者 1

If it's something like above, how would I search for a participant pair? Could I order the participant IDs in some way so that they are always participant1 < participant2 for example, making search simpler?

推荐答案

好吧,这个问题没有正确答案,但可以肯定的是,你提到的方法根本不是最好的!

Well, there is no correct answer to this question, But definitely, the approaches you have mentioned are not the best at all!

首先,当你在考虑设计一个聊天"模型时,你需要考虑到用户之间会有数百万条消息,所以当你想要获取消息时,你需要关心性能聊天.

将消息存储到数组中根本不是一个好主意,到时候您的模型的大小会很大,您必须考虑到 MongoDB 的文档大小限制目前是每个文档 16 MB.

Storing the messages into an array is not a good idea at all, your model's size will be large by the time and you have to consider that MongoDB's document size limit is currently 16 MB per document.

https://docs.mongodb.com/manual/reference/limits/

其次,你必须考虑分页方面,因为当聊天量很大时会影响性能,当你检索2个用户之间的聊天时,你不会请求从时间开始的所有聊天,你只会请求最新的,然后如果用户滚动聊天,您可以请求较旧的,这方面非常重要,并且由于它对性能的影响而不能被忽视.

我的方法是将每条消息存储在一个单独的文档中

首先,将每条消息存储在单个文档中会提高您在获取聊天记录时的性能,并且文档大小会非常小.

First of all, storing each message in a single document will boost your performance during fetching the chats, and the document size will be very small.

这是一个很简单的例子,你需要根据自己的需要改变模型,只是为了表达想法:

const MessageSchema = mongoose.Schema({
    message:{
        text: { type:String, required:true }
        // you can add any other properties to the message here.
        // for example, the message can be an image ! so you need to tweak this a little
    }
    // if you want to make a group chat, you can have more than 2 users in this array
    users:[{
        user: { type:mongoose.Schema.Types.ObjectId, ref:'User', required:true }
    }]
    sender: { type:mongoose.Schema.Types.ObjectId, ref:'User', required:true },
    read: { type:Date }
},
{
    timestamps: true
});

您可以通过此查询获取聊天记录:

 Message.find(({ users: { "$in" : [#user1#,#user2#]} })
    .sort({ updatedAt: -1 })
    .limit(20)

简单又干净!如您所见,使用这种方法进行分页变得非常容易.

Easy and clean! as you see, pagination becomes very easy with this approach.

这篇关于Mongoose 私聊消息模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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