我的用户架构中有一个通知对象数组。我想让每条通知在x时间后过期。那可能吗?我遵循的其他答案给了我错误。
我尝试添加createdAt: {type: Date, default: Date.now, expires: 7200}
到我的用户架构和通知推送数组。 “类型:日期”似乎是在破坏它。
const UserSchema = mongoose.Schema({
name: {
type: String,
required: true
},
uuid: {
type: String,
required: true
},
keep: {
type: String
},
notifications: [{notifcationType: String, date: String, user: String, _id: false, createdAt: {type: Date, default: Date.now, expires: 7200} }],
followers: [{user: String, date: String, _id: false}],
following: [{user: String, date: String, _id: false}]
});
const User = module.exports = mongoose.model('User', UserSchema);
//my function to push notifications that a user followed. Works without the createdAt object
module.exports.addFollowNotify = (id, user, options, callback) => {
const query = {username: user.following}
let update = {
$push: {notifications: {user: user.username, date: user.date, notifcationType: "Follow", createdAt: {type: Date, default: Date.now, expires: 7200} }}
};
User.findOneAndUpdate(query, update, options, callback);
};
答案 0 :(得分:0)
您应该必须创建“通知”的新集合并参考userid创建新的通知。这样,您可以阅读用户的通知,通知将根据到期时间删除。