- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在 Rails 中构建一个论坛系统,以便更加熟悉 Rails 和 Mongoid。我想添加的一个功能是一个私有(private)消息系统论坛用户可以用来互相发送消息。在架构设计方面,我可以想到两种解决方案:
用户和消息是使用“has_many”和“belongs_to”相互链接的单独文档。
User document
has_many :messages_sent, :class_name => 'Message', :inverse_of => :message_sender
has_many :messages_received, :class_name => 'Message', :inverse_of => :message_recipient
和
Message Document
field :created, type: DateTime, default: -> { Time.now }
field :content, type: String
belongs_to :message_sender, :class_name => 'User', :inverse_of => :messages_sent
belongs_to :message_recipient, :class_name => 'User', :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 集合,并且需要对发送和接收的消息进行大量手动过滤和交错处理。努力。
在对话文档中嵌入消息。我将在下面提供用户、消息和对话的代码。一个对话通过 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
最佳答案
我了解到您使用的是 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
https://groups.google.com/forum/?fromgroups=#!topic/mongoid/BOBqhYLb7O0
我在几个项目中使用 MongoId 有一个内部消息传递系统,并使用第一个解决方案。
如果你添加了其他类"Conversation"
你不应该嵌入消息,因为一个 session 可以有无限数量的消息。你应该使用 has_may messages
和 belongs_to conversation
。
我认为这两种解决方案都很好,因此您可以根据项目逻辑选择您的需求。如果您的逻辑更简单,您可以选择第一个解决方案。否则,如果您的逻辑更复杂,请选择后一种解决方案。
问候!
关于ruby-on-rails - 使用 Mongoid 的 MongoDB 对话/私有(private)消息模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499180/
对此感到疯狂,真的缺少一些东西。 我有webpack 4.6.0,webpack-cli ^ 2.1.2,所以是最新的。 在文档(https://webpack.js.org/concepts/mod
object Host "os.google.com" { import "windows" address = "linux.google.com" groups = ["linux"] } obj
每当我安装我的应用程序时,我都可以将数据库从 Assets 文件夹复制到 /data/data/packagename/databases/ .到此为止,应用程序工作得很好。 但 10 或 15 秒后
我在 cc 模式缓冲区中使用 hideshow.el 来折叠我不查看的文件部分。 如果能够在 XML 文档中做到这一点就好了。我使用 emacs 22.2.1 和内置的 sgml-mode 进行 xm
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
根据java: public Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a patt
我读过一些关于 PRG 模式以及它如何防止用户重新提交表单的文章。比如this post有一张不错的图: 我能理解为什么在收到 2xx 后用户刷新页面时不会发生表单提交。但我仍然想知道: (1) 如果
看看下面的图片,您可能会清楚地看到这一点。 那么如何在带有其他一些 View 的简单屏幕中实现没有任何弹出/对话框/模式的微调器日期选择器? 我在整个网络上进行了谷歌搜索,但没有找到与之相关的任何合适
我不知道该怎么做,我一直遇到问题。 以下是代码: rows = int(input()) for i in range(1,rows): for j in range(1,i+1):
我想为重写创建一个正则表达式。 将所有请求重写为 index.php(不需要匹配),它不是以/api 开头,或者不是以('.html',或'.js'或'.css'或'.png'结束) 我的例子还是这样
MVC模式代表 Model-View-Controller(模型-视图-控制器) 模式 MVC模式用于应用程序的分层开发 Model(模型) - 模型代表一个存取数据的对象或 JAVA PO
我想为组织模式创建一个 RDF 模式世界。您可能知道,组织模式文档基于层次结构大纲,其中标题是主要的分组实体。 * March auxiliary :PROPERTIES: :HLEVEL: 1 :E
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含“person”——一个值为对象数组的对象。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON
假设我有 4 张 table 人 公司 团体 和 账单 现在bills/persons和bills/companys和bills/groups之间是多对多的关系。 我看到了 4 种可能的 sql 模式
假设您有这样的文档: doc1: id:1 text: ... references: Journal1, 2013, pag 123 references: Journal2, 2014,
我有这个架构。它检查评论,目前工作正常。 var schema = { id: '', type: 'object', additionalProperties: false, pro
这可能很简单,但有人可以解释为什么以下模式匹配不明智吗?它说其他规则,例如1, 0, _ 永远不会匹配。 let matchTest(n : int) = let ran = new Rand
我有以下选择序列作为 XML 模式的一部分。理想情况下,我想要一个序列: 来自 my:namespace 的元素必须严格解析。 来自任何其他命名空间的元素,不包括 ##targetNamespace和
我希望编写一个 json 模式来涵盖这个(简化的)示例 { "errorMessage": "", "nbRunningQueries": 0, "isError": Fals
首先,我是 f# 的新手,所以也许答案很明显,但我没有看到。所以我有一些带有 id 和值的元组。我知道我正在寻找的 id,我想从我传入的三个元组中选择正确的元组。我打算用两个 match 语句来做到这
我是一名优秀的程序员,十分优秀!