猫鼬增量或设置为最大

发布于 2025-01-23 14:32:28 字数 793 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清眉祭 2025-01-30 14:32:28

您可以使用$ cond执行检查。

db.collection.update({
  userId: <userId>
},
[
  {
    "$addFields": {
      "likeLimit": {
        "$cond": {
          "if": {
            $gt: [
              {
                "$add": [
                  "$likeLimit",
                  <LIKE_LIMIT_INCREASE>
                ]
              },
              <LIKE_LIMIT_CAP>
            ]
          },
          "then": <LIKE_LIMIT_CAP>,
          "else": {
            "$add": [
              "$likeLimit",
              <LIKE_LIMIT_INCREASE>
            ]
          }
        }
      }
    }
  }
],
{
  multi: true
})

这是 mongo playground 供您参考。

You can use $cond to perform the checking.

db.collection.update({
  userId: <userId>
},
[
  {
    "$addFields": {
      "likeLimit": {
        "$cond": {
          "if": {
            $gt: [
              {
                "$add": [
                  "$likeLimit",
                  <LIKE_LIMIT_INCREASE>
                ]
              },
              <LIKE_LIMIT_CAP>
            ]
          },
          "then": <LIKE_LIMIT_CAP>,
          "else": {
            "$add": [
              "$likeLimit",
              <LIKE_LIMIT_INCREASE>
            ]
          }
        }
      }
    }
  }
],
{
  multi: true
})

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文