如何根据 req.body 发送的多个条件更新文档的多个字段(maxLength、maxBreadth 或 maxArea)?

发布于 2025-01-19 04:23:04 字数 928 浏览 0 评论 0原文

我想存储从请求遇到的最大长度,宽,区域,并将其存储在数据库中。在这种情况下,任何内容都可以最大,并且基于我要在数据库中仅更新该特定字段的最大值。

const body = {
  oID: 123, // Primary key
  length: 50,
  breadth: 50
};
const { length, breadth } = body;
const currentArea = length * breadth;
await totalArea.updateOne(
  { oID: 123 },
  {
    $set: { $maxLength: maxLength < length ? length : $maxLength }, // I want to update the docs based on mongo query only since this won't work.
    $set: { $maxBreadth: maxBreadth < breadth ? breadth : $maxBreadth },  // I want to update the docs based on mongo query only since this won't work.
    $set: { $maxArea: maxArea < currentArea ? currentArea : $maxArea }  // I want to update the docs based on mongo query only since this won't work.
  },
  { upsert: true }
);

在上面的示例中,我已经使用三元运算符演示了逻辑,我想使用MongoDB查询执行相同的操作。 IE在将其与数据库中的现有字段进行比较时更新特定字段,并在满足条件的情况下对其进行更新。

I want to store maximum length, breadth, area ever encountered from a request and store it in the database. In this case, anything can be maximum and based on that I want to update max values in the database for that particular field only.

const body = {
  oID: 123, // Primary key
  length: 50,
  breadth: 50
};
const { length, breadth } = body;
const currentArea = length * breadth;
await totalArea.updateOne(
  { oID: 123 },
  {
    $set: { $maxLength: maxLength < length ? length : $maxLength }, // I want to update the docs based on mongo query only since this won't work.
    $set: { $maxBreadth: maxBreadth < breadth ? breadth : $maxBreadth },  // I want to update the docs based on mongo query only since this won't work.
    $set: { $maxArea: maxArea < currentArea ? currentArea : $maxArea }  // I want to update the docs based on mongo query only since this won't work.
  },
  { upsert: true }
);

In the above example, I have demonstrated the logic using ternary operator and I want to perform the same operation using mongoDB query. i.e update a particular field while comparing it to existing fields from the database and update it if it satisfies the condition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

茶底世界 2025-01-26 04:23:04
await totalArea.updateOne(
  { oID: 123
      },
      {
        $max: {
          maxLength: length,
          maxBreadth: breadth,
          maxArea : currentArea
        }
      },
      { upsert: true }
    );

我发现这工作正常。感谢@Mehari Mamo的评论。

await totalArea.updateOne(
  { oID: 123
      },
      {
        $max: {
          maxLength: length,
          maxBreadth: breadth,
          maxArea : currentArea
        }
      },
      { upsert: true }
    );

I found this to be working correctly. Thanks to @Mehari Mamo's comment.

清风夜微凉 2025-01-26 04:23:04

如果我正确理解,您需要这样的东西:

db.collection.update({
  oID: 123
},
[{$set: {maxLength: {$max: [length, "$maxLength"]},
         maxBreadth: {$max: [breadth, "$maxBreadth"]},
         maxArea : {$max: [currentArea, "$maxArea "]}
      }
    }
],
{
  upsert: true
})

您可以检查在这里

第二步上的[]允许您访问文档字段的当前值,因为这是一个聚合管道。

If I understand correctly, you want something like this:

db.collection.update({
  oID: 123
},
[{$set: {maxLength: {$max: [length, "$maxLength"]},
         maxBreadth: {$max: [breadth, "$maxBreadth"]},
         maxArea : {$max: [currentArea, "$maxArea "]}
      }
    }
],
{
  upsert: true
})

You can check it here .

The [] on the second step allows you to access the current values of the document fields, as this is an aggregation pipeline.

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