[英]How to Add Notifications in mongoose?
I have this user schema我有这个用户架构
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");
const userSchema = new Schema(
{
email: {
type: String,
required: true,
index: {
unique: true
}
},
password: {
type: String,
required: true
},
name: {
type: String,
required: true
},
website: {
type: String
},
bio: {
type: String
}
},
{
timestamps: {
createdAt: "created_at",
updatedAt: "updated_at"
},
toJSON: { virtuals: true }
}
);
userSchema.virtual("blogs", {
ref: "Blog",
localField: "_id",
foreignField: "author"
});
userSchema.pre("save", function(next) {
const user = this;
if (!user.isModified("password")) return next();
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
userSchema.methods.comparePassword = function(password, next) {
bcrypt.compare(password, this.password, function(err, isMatch) {
if (err) return next(err);
next(null, isMatch);
});
};
const User = mongoose.model("User", userSchema);
module.exports = User;
I want to send notification to everyone when a user creates a blog or add a comment how can I implement this?我想在用户创建博客或添加评论时向所有人发送通知,我该如何实现? should I use triggers?
我应该使用触发器吗?
The strategy behind this这背后的策略
I guess this can be easily achieved by using mongodb change streams.我想这可以通过使用 mongodb 更改流轻松实现。
You can see more about here with code examples.您可以通过代码示例查看更多关于此处的信息。
power of mongodb change streams mongodb 改变流的力量
Basically listen for insert/update operation type and react accordingly.基本上监听插入/更新操作类型并做出相应的反应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.