You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于MongoDB与Node.js(Express)的通知流程Schema设计咨询

MongoDB + Express 通知流程 Schema 设计方案

核心集合设计

一共建议定义4个核心集合,覆盖模板配置、发送任务、用户配置、历史归档全流程:

1. 通知模板集合(notification_templates)

用来标准化不同类型、不同渠道的通知内容,避免硬编码文案,后续修改通知内容无需调整业务代码。
Mongoose Schema 示例:

const notificationTemplateSchema = new mongoose.Schema({
  // 通知类型,唯一标识,如sms_verify、order_paid、system_announce
  type: { type: String, required: true, unique: true },
  // 通知所属渠道:sms(短信)/email(邮件)/push(APP推送)/site_msg(站内信)
  channel: { type: String, required: true, enum: ['sms', 'email', 'push', 'site_msg'] },
  // 标题模板,站内信/邮件/推送必填
  titleTemplate: { type: String },
  // 内容模板,用{{变量名}}占位,如"您的验证码是{{code}},5分钟内有效"
  contentTemplate: { type: String, required: true },
  // 模板需要传入的变量列表,用于业务端校验参数
  variables: { type: [String], default: [] },
  // 模板是否启用
  isActive: { type: Boolean, default: true },
  createTime: { type: Date, default: Date.now },
  updateTime: { type: Date, default: Date.now }
})

2. 通知任务集合(notification_tasks)

记录每一条待发送/已发送的通知任务,跟踪全生命周期状态,支持重试逻辑。
Mongoose Schema 示例:

const notificationTaskSchema = new mongoose.Schema({
  // 关联对应模板id
  templateId: { type: mongoose.Schema.Types.ObjectId, ref: 'NotificationTemplate', required: true },
  // 接收方信息,按渠道传对应字段:userId(站内信必填)、phone(短信必填)、email(邮件必填)、deviceId(推送必填)
  recipient: { type: Object, required: true },
  // 填充模板的实际变量键值对,如{code: '123456'}
  variables: { type: Object, default: {} },
  // 冗余存储渠道,避免每次都关联查询模板
  channel: { type: String, required: true },
  // 优先级:1最高(验证码、安全提醒),2中等(订单通知),3最低(营销通知)
  priority: { type: Number, default: 2, min: 1, max: 3 },
  // 定时发送时间,立即发送则填当前时间
  scheduleTime: { type: Date, default: Date.now },
  // 任务状态
  status: { type: String, required: true, enum: ['pending', 'sending', 'success', 'failed', 'cancelled'], default: 'pending' },
  // 失败原因,发送失败时写入
  failReason: { type: String },
  // 已重试次数,最多重试3次可按业务调整
  retryCount: { type: Number, default: 0 },
  // 实际发送完成时间
  sendTime: { type: Date },
  createTime: { type: Date, default: Date.now }
})
// 建立索引优化查询:按状态+优先级+调度时间查询待发送任务
notificationTaskSchema.index({ status: 1, priority: 1, scheduleTime: 1 })

3. 用户通知配置集合(user_notification_settings)

存储用户自定义的通知接收开关,发送前校验避免打扰用户。
Mongoose Schema 示例:

const userNotificationSettingSchema = new mongoose.Schema({
  userId: { type: mongoose.Schema.Types.ObjectId, required: true },
  // 对应通知渠道,all代表所有渠道
  channel: { type: String, required: true, default: 'all' },
  // 对应通知类型,all代表所有类型
  notificationType: { type: String, required: true, default: 'all' },
  // 是否允许接收
  enabled: { type: Boolean, default: true },
  updateTime: { type: Date, default: Date.now }
})
// 联合唯一索引,避免同一个用户同渠道同类型出现重复配置
userNotificationSettingSchema.index({ userId: 1, channel: 1, notificationType: 1 }, { unique: true })

4. 已发送通知归档集合(sent_notification_archives)

归档发送成功的通知,冷存历史数据,同时支持用户查询站内信历史、已读状态。
Mongoose Schema 示例:

const sentNotificationArchiveSchema = new mongoose.Schema({
  // 冗余存储原任务的全部核心字段,避免关联查询
  taskId: { type: mongoose.Schema.Types.ObjectId, required: true },
  userId: { type: mongoose.Schema.Types.ObjectId },
  channel: { type: String, required: true },
  title: { type: String },
  content: { type: String, required: true },
  sendTime: { type: Date, required: true },
  // 站内信专属字段:已读状态
  readStatus: { type: String, enum: ['unread', 'read'], default: 'unread' },
  readTime: { type: Date }
})
// 索引优化用户站内信查询
sentNotificationArchiveSchema.index({ userId: 1, sendTime: -1 })

额外扩展说明

  • 如果需要支持全量用户批量通知(如系统公告),可新增batch_notification_jobs集合,记录批量任务的总数量、已发送数量、状态等信息,每个子任务依然存入notification_tasks关联批量任务id即可
  • 发送队列可以用BullMQ等工具配合实现,按优先级调度任务,失败的任务根据优先级设置不同的重试间隔
  • 发送通知前必须先校验对应用户的user_notification_settings配置,用户关闭的通知类型直接跳过发送

内容的提问来源于stack exchange,提问作者Surbhi Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 08:36:07