У меня есть массив объектов уведомлений внутри моей пользовательской схемы. Я хочу, чтобы срок действия каждого уведомления истек, скажем, через 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);
};