我在设置正确的会话模式时遇到问题,该模式存储会话和/或消息的已读/未读标志。我希望当前登录的用户能够看到有多少会话中有新消息。目前,我的架构(使用implements-schema)看起来有点像这样:
Schema = {};
Schema.ConversationMessageRead = new SimpleSchema(
{
"userId": {
type: String
},
"createdAt": {
type: Date
}
}
);
Schema.ConversationMessage = new SimpleSchema(
{
"userId": {
type: String
},
"message": {
type: String,
optional: true
},
"read": {
type: [Schema.ConversationMessageRead],
optional: true
},
"createdAt": {
type: Date
}
}
);
Schema.Conversation = new SimpleSchema(
{
"participants": {
type: [String],
optional: false
},
"messages": {
type: [Schema.ConversationMessage],
optional: true
},
"deleted": {
type: Boolean,
optional: false,
defaultValue: false
},
"createdAt": {
type: Date
}
}
);
Conversations = new Mongo.Collection("conversations");
Conversations.attachSchema(Schema.Conversation);
我的想法是在打开对话时使用当前用户ID更新所有邮件子文档read
属性。然后我很快发现你现在不能update multiple subdocuments。
如果有人知道如何以正确的方式实现这一目标,我愿意改变对话系统的管理方式。要记住的另一件事是我想让它保持反应,这意味着我必须在不使用聚合函数的情况下查询此集合。
答案 0 :(得分:0)
执行此操作的一种方法是拥有事件日志集合:
Schema.EventLog = new SimpleSchema({
userId: { type: String },
conversationMessageId: { type: String },
createdAt: { type: Date }
});
此集合中的文档永远不会被修改(插入,然后是只读),这很快。
虽然这确实提出了一个问题,但如果您要将所有conversationMessages
设置为一次读取,那么为什么不保存用户阅读对话的最后一个日期时间,然后您可以查看是否有任何消息在那次谈话中比上次阅读日期更新?完成相同目的的数据要少得多。
答案 1 :(得分:0)
如果我理解正确,这不是多次更新,而是每次用户查看帖子时对子文档数组的一次添加?我认为这个命令将达到预期的效果:
Conversations.update(
{ _id: CONVERSATION_ID },
{
$push: {
'messages.read': NEW_CONVERSATION_MESSAGE_READ_OBJECT }
}
)