Mongoose,删除过期的引用

时间:2016-12-19 21:47:31

标签: javascript node.js mongodb mongoose

我有两个集合,公会和logentries,对logentries的引用在'guild.mod_log.entries'中保存为数组。文档成功过期,但'mod_log.entries'中的引用不会被删除。

所以我只是想知道如何删除那些过期的引用。

以下是架构部分

// guilds.js
module.exports = new mongoose.Schema({
	_id: {type: String, required: true},
	members: [require('./guildMember')],
	mod_log: require('./guildModLog'),
	default_role: String
});

// guildModLog.js
module.exports = {
	enabled: {type: Boolean, default: false},
	channel_id: {type: String},
	entries: [{type: mongoose.Schema.Types.ObjectId, ref: require('./modLogEntry')}]
};

// modLogEntry.js
module.exports = new Schema({
	action: {type: String, required: true, enum: ['Ban', 'Unban', 'Kick', 'Warn']},
	timestamp: {type: Date, required: true, expires: '5s'},
	user: {type: String, required: true},
	staff: {type: String, required: true},
	reason: {type: String, required: true, minlength: 5, maxlength: 100},
});

1 个答案:

答案 0 :(得分:0)

您可以重构架构,以便公会和条目之间的链接是外键。

(Bit of code that determines the domain used)
$conn = connectDB();
$result = $conn->query('UPDATE hitCounter SET hits = hits + 1 WHERE domain = "'.$domain.'"');
$conn->close();

这样做意味着您不能使用// guildModLog.js module.exports = { enabled: {type: Boolean, default: false}, channel_id: {type: String} }; // modLogEntry.js module.exports = new Schema({ _guild_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Guilds', index: true } action: {type: String, required: true, enum: ['Ban', 'Unban', 'Kick', 'Warn']}, timestamp: {type: Date, required: true, expires: '5s'}, user: {type: String, required: true}, staff: {type: String, required: true}, reason: {type: String, required: true, minlength: 5, maxlength: 100}, }); 来获取公会的mod日志条目,但它会使所有数据在一个集合中保持过期。编写简单的.populate()比尝试使非TTL集合中的数据过期更容易。