[英]Mongoose Incrament Increase Entire Object Array
我正在尝试开始保存用户的统计信息(对于不和谐的bot),例如命令用法等。
我想将所有内容保存在mongodb
。 基本上,我正在尝试从可节省用户命令使用率的数组中完成定期更新db的功能,然后在数据库上更新信息后清除/擦除。
我的架构如下:
const userStatsSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
userID: String,
commandUsage: {
command1: {
type: Number,
default: 0
},
command2: {
type: Number,
default: 0
},
command3: {
type: Number,
default: 0
},
command4: {
type: Number,
default: 0
},
command5: {
type: Number,
default: 0
}
}
})
const UserStats = mongoose.model('global_users', userStatsSchema);
老实说,关于一次更新所有内容,我不知道从何处去。 请记住, commandUsage
数组要比它大得多,并且commandUsage
不断增长。
我需要单独增加一个,还是有更好的方法?
$inc: commandUsage: { command1: 5, command2: 8, command3: 2, command4: 0 }
任何帮助弄清楚这一点将不胜感激!
您可以使用批量操作以固定的时间间隔(时间或发生次数)执行
const UserStats = mongoose.model( 'global_users' );
const bulk = UserStats.collection.initializeOrderedBulkOp();
const maxNbrOfCommandAtOnce = 1000; // if you want to use an interval based on the nbr of commands
const interval = 10000; // to set an interval of 10 sec
let saveBulkInterval = setInterval(executeBatch, interval);
const onCommand = function(event) {
// I guess here that you have the ID of the item you to modify
bulk.find({_id: itemId}).update({$inc: {event.commandName: 1}});
if (bulk.s.currentBatchSize === maxNbrOfCommandAtOnce)
executeBatch();
};
const executeBatch = function() {
if (bulk.s.currentBatchSize > 0) {
bulk.execute(function() {
bulk = UserStats.collection.initializeOrderedBulkOp();
});
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.