猫鼬增量或设置为最大
我有一个mongoose架构,每2小时的上限/最大为20个
//The maximum amount of users that a user can receive
export const LIKE_LIMIT_CAP = 20
//The amount of likes given every two hours to the user
export const LIKE_LIMIT_INCREASE = 5
@Schema()
export class LikeLimit extends Document {
@Prop({ required: true })
userId: number
@Prop({ required: true, default: 0, max: LIKE_LIMIT_CAP, min: 0 })
likeLimit: number
@Prop({ default: null })
lastLikedDate: Date
}
小时,我有一个cron工作,我想将值增加5个,但是,如果值超过20个,则应将其设置为20
。 Mongo查询应该具有以下逻辑:
if(likeLimit >= LIKE_LIMIT_CAP - LIKE_LIMIT_INCREASE){
likeLimit = LIKE_LIMIT_CAP
}else{
likeLimit = likeLimit + LIKE_LIMIT_INCREASE
}
这种查询是否可以在Mongoose中?
I have a mongoose schema, with a cap/max of 20
//The maximum amount of users that a user can receive
export const LIKE_LIMIT_CAP = 20
//The amount of likes given every two hours to the user
export const LIKE_LIMIT_INCREASE = 5
@Schema()
export class LikeLimit extends Document {
@Prop({ required: true })
userId: number
@Prop({ required: true, default: 0, max: LIKE_LIMIT_CAP, min: 0 })
likeLimit: number
@Prop({ default: null })
lastLikedDate: Date
}
Every 2 hours I have a cron job, where I want to increment the value by 5, however, if the value exceeds 20, it should simply be set to 20.
Ideally, the mongo query should have the following logic:
if(likeLimit >= LIKE_LIMIT_CAP - LIKE_LIMIT_INCREASE){
likeLimit = LIKE_LIMIT_CAP
}else{
likeLimit = likeLimit + LIKE_LIMIT_INCREASE
}
Is this kind of query possible in Mongoose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ cond
执行检查。这是 mongo playground 供您参考。
You can use
$cond
to perform the checking.Here is the Mongo playground for your reference.