使用 Mongoid 的 MongoDB 对话/私人消息模式

Posted

技术标签:

【中文标题】使用 Mongoid 的 MongoDB 对话/私人消息模式【英文标题】:MongoDB Conversation / Private Message Schema using Mongoid 【发布时间】:2013-01-08 01:57:06 【问题描述】:

我正在 Rails 中构建一个论坛系统,以便更加熟悉 Rails 和 Mongoid。我想添加的一个功能是一个私人消息系统论坛用户可以用来互相发送消息。 在架构设计方面,我可以想到两种解决方案:

解决方案 1

用户和消息是使用“has_many”和“belongs_to”相互链接的单独文档。

用户文档

has_many :messages_sent, :class_name => 'Message', :inverse_of => :message_sender

has_many :messages_received, :class_name => 'Message', :inverse_of => :message_recipient

消息文档

字段:created,类型:DateTime,默认值:-> Time.now

字段:内容,类型:字符串

belongs_to :message_sender, :class_name => '用户', :inverse_of => :messages_sent

belongs_to :message_recipient, :class_name => '用户', :inverse_of => :messages_received

为了向用户显示他的收件箱,我会查看由:created 排序的some_user.messages_received 并进行过滤,因此我有一个唯一发件人 ID 列表,按他们最后一封邮件发送到 some_user 的时间排序。

然后为了显示一个特定的对话,我只需获取两个参与者之间的所有消息并根据时间戳将它们交错:

messages_in = some_user.messages_received.where(:message_sender => selected_correspondent)

messages_out = some_user.messages_sent.where(:message_recipient => selected_correspondent)。

我不喜欢这种解决方案,因为它涉及多次使用“where”查询来访问 Messages 集合,并且需要对发送和接收的消息进行大量手动过滤和交错。努力。

解决方案 2(我现在正在使用)

在对话文档中嵌入消息。我将在下面提供用户、消息和对话的代码。一个对话通过has_and_belongs_to_many 链接到两个或多个用户(n-n,因为一个用户也可能有许多对话)。这也可能允许多用户对话。

我喜欢这个解决方案,因为为了向用户显示他的收件箱,我可以使用由:last_message_received 排序的some_user.conversations 在对话文档中存储和更新,无需过滤。为了显示特定的对话,我不需要交错发送和接收的消息,因为消息已经以正确的顺序嵌入到对话文档中。

此解决方案的唯一问题是在您要添加消息时找到由两个(或更多)用户共享的正确对话文档。这里建议了一种解决方案:mongodb conversation system,但我不喜欢它,因为查询似乎相对昂贵,而且多用户对话的扩展看起来会很棘手。相反,我在对话文档中有一个名为 :lookup_hash 的字段,它是根据参与对话的每个用户的对象 ID 计算得出的 SHA1 哈希值。这样,给定两个或更多用户,找到他们对应的对话文档(或者如果它还不存在就创建它)是很容易的。

要将消息添加到对话中,我只需使用Conversation.add_message(类方法,而不是实例方法,因为对话可能还不存在)给它一个发件人、收件人和新的消息对象。

问题

我的问题是:考虑到 Mongoid(或者一般来说只是 NoSQL)架构设计最佳实践,我是否做任何明显错误的事情?我能做些什么来改进我的解决方案吗?我使用散列查找对话的想法是个坏主意吗?

代码

user.rb

class User
  include Mongoid::Document

  field :username, type: String
  field :joined, type: DateTime, default: -> Time.now 
  field :last_activity, type: DateTime, default: ->  Time.now 

  has_and_belongs_to_many :conversations 
end

conversation.rb

require 'digest/sha1'

class Conversation
  include Mongoid::Document

  field :lookup_hash, type: String
  field :created, type: DateTime, default: ->  Time.now 
  field :last_message_time, type: DateTime, default: ->  Time.now 
  # Array of user ids of users that have read all messages in this conversation
  field :last_message_seen_by, type: Array, default: []

  embeds_many :messages
  has_and_belongs_to_many :participants, :class_name => 'User'

  validates_presence_of :lookup_hash

  index( lookup_hash: 1 ,  unique: true, name: "lookup_hash_index" )
    # Used to show a user a list of conversations ordered by last_message_time
  index( _id: 1, last_message_time: -1 ,  unique: true, name: "id_last_message_time_index" )

  def self.add_message(recipient, sender, message)
    # Find or create a conversation:
    conversation = Conversation.find_or_create_by(
      :lookup_hash => get_lookup_hash([recipient.id, sender.id])) do |c|
        c.participants.concat [recipient, sender]
      end
    conversation.messages << message
    conversation.last_message_time = Time.now
    conversation.last_message_seen_by.delete(recipient)
    conversation.save
  end

  private
    def self.get_lookup_hash(participant_ids)
      lookup_key = participant_ids.sort.join(':')
      Digest::SHA1.hexdigest lookup_key
    end
end

message.rb

class Message
  include Mongoid::Document

  field :created, type: DateTime, default: ->  Time.now 
  field :text, type: String

  embedded_in :conversation
  belongs_to :author, :class_name => 'User'

  validates_length_of :text, minimum: 2, maximum: 256
  validates_presence_of :author
end

【问题讨论】:

【参考方案1】:

我了解到您使用的是 MongoId 3.0。我在您的第一个解决方案中没有发现任何问题:

messages_in = some_user.messages_received.where(:message_sender => current_user)

messages_out = some_user.messages_sent.where(:message_recipient => current_user).

您可以找到各种示例:

Preferred way to private messages modeling in Rails 3

http://pastebin.com/fKavivbp

https://groups.google.com/forum/?fromgroups=#!topic/mongoid/BOBqhYLb7O0

我在几个项目中使用 MongoId 有一个内部消息传递系统,并使用第一个解决方案。

如果你添加其他类"Conversation"你不应该嵌入消息,因为一个对话可以有无限数量的消息。你应该使用has_may messagesbelongs_to conversation

我认为这两种解决方案都很好,因此您可以根据项目逻辑选择您的需求。如果您的逻辑更简单,您可以选择第一个解决方案。否则,如果您的逻辑更复杂,请选择后一种解决方案。

问候!

【讨论】:

是的,MongoId 3.0。很高兴您指出,如果我在对话中嵌入消息,由于 Mongo 对文档大小的限制,对话可能包含的最大消息数是有限制的。要使用解决方案 2,我必须限制对话中的消息数量(尽管限制可能非常高,在成千上万条消息的范围内)。

以上是关于使用 Mongoid 的 MongoDB 对话/私人消息模式的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Mongoid 查看原始 mongoDB 查询

如何将内存中的 MongoDB 与 Rails、Mongoid 和 Rspec 一起使用?

批量查找 mongoDB 记录(使用 mongoid ruby​​ 适配器)

在非 Rails 环境中通过 Mongoid 创建 MongoDB 索引

Ruby on Rails 是 mongodb - mongoid

如何实现has_many:通过与Mongoid和mongodb的关系?