express/mongodb日期格式以匹配req.params到蒙戈文档中

发布于 2025-01-23 02:24:52 字数 1097 浏览 0 评论 0原文

我有一个存储日期的MongoDB系列(落地)。

在我的明确路由中,我希望将日期格式化为mm-dd-yyyy,以便我可以将其用于URL。该URL将找到所有具有匹配日期和匹配ResortNames的文档。

dateRouter.get("/:formattedDate", (req, res) => {
  const formattedDate = req.params.formattedDate;

  SlopeDay.find({}) // isolate dates
    .then((dateObj) => {
      dateObj.forEach((date, i) => {
        let dateStr =
          // MM-DD-YYYY reformatting to string
          ("0" + (date.date.getMonth() + 1)).slice(-2) +
          "-" +
          ("0" + date.date.getDate()).slice(-2) +
          "-" +
          date.date.getFullYear();
         // map below doesn't seem to be doing much
        const objWithFormattedDate = dateObj.map((obj) => {
          return { ...obj, formattedDate: dateStr, isNew: true };
        });
        // console.log(objWithFormattedDate);
      });
    });
});

我为如何正确执行此操作而感到不知所措。我需要获取路线,以访问将日期与MM-DD-Yyyy参数URL相匹配的所有落地文档。

I have a MongoDB collection (SlopeDay) that has dates stored.

enter image description here

In my express routing, I'm looking to format the date to MM-DD-YYYY so that I can use that for the URL. That URL will find all documents with matching dates AND matching resortNames.

dateRouter.get("/:formattedDate", (req, res) => {
  const formattedDate = req.params.formattedDate;

  SlopeDay.find({}) // isolate dates
    .then((dateObj) => {
      dateObj.forEach((date, i) => {
        let dateStr =
          // MM-DD-YYYY reformatting to string
          ("0" + (date.date.getMonth() + 1)).slice(-2) +
          "-" +
          ("0" + date.date.getDate()).slice(-2) +
          "-" +
          date.date.getFullYear();
         // map below doesn't seem to be doing much
        const objWithFormattedDate = dateObj.map((obj) => {
          return { ...obj, formattedDate: dateStr, isNew: true };
        });
        // console.log(objWithFormattedDate);
      });
    });
});

I'm at a loss for how to do this properly. I need the get route to access all SlopeDay documents matching dates to the MM-DD-YYYY parameter URL.

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

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

发布评论

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

评论(2

网名女生简单气质 2025-01-30 02:24:52

我能够通过以这种方式打破字符串并查询来使其正常工作:

dateRouter.get("/:formattedDate", (req, res) => {
  const formattedDate = req.params.formattedDate;

  // break up the date
  const targetChars = formattedDate.substring(3, 5);
  const beforeTargetChar = formattedDate.substring(0, 3);
  const afterTargetChar = formattedDate.substring(5);

  // create lower and upper boundaries that straddle the formatted date
  const lowerbound = beforeTargetChar + (targetChars - 1) + afterTargetChar;
  const upperbound =
    beforeTargetChar + (Number(targetChars) + 1) + afterTargetChar;

  SlopeDay.find({
    date: {
      // find docs with dates between the boundaries (THIS SHOULD EQUAL req.params.formattedDate)
      $gte: new Date(lowerbound),
      $lt: new Date(upperbound),
    }, // add 2nd query here
  }).then((dateData) => res.send(dateData));
});

I'm able to get it working by breaking the strings up and querying that way:

dateRouter.get("/:formattedDate", (req, res) => {
  const formattedDate = req.params.formattedDate;

  // break up the date
  const targetChars = formattedDate.substring(3, 5);
  const beforeTargetChar = formattedDate.substring(0, 3);
  const afterTargetChar = formattedDate.substring(5);

  // create lower and upper boundaries that straddle the formatted date
  const lowerbound = beforeTargetChar + (targetChars - 1) + afterTargetChar;
  const upperbound =
    beforeTargetChar + (Number(targetChars) + 1) + afterTargetChar;

  SlopeDay.find({
    date: {
      // find docs with dates between the boundaries (THIS SHOULD EQUAL req.params.formattedDate)
      $gte: new Date(lowerbound),
      $lt: new Date(upperbound),
    }, // add 2nd query here
  }).then((dateData) => res.send(dateData));
});

旧瑾黎汐 2025-01-30 02:24:52

只需使用JavaScript,我就可以推荐这篇文章可能会有所帮助

否则,您也可以使用许多图书馆来执行此操作。我个人喜欢使用day.js。他们的格式函数看起来像这样的东西,应该适合您的需求等等如果您想走那条路。

dayjs(YourDateHere).format('MM-DD-yyyy')

Cheers!

Just using Javascript I can recommend this post that might help

Otherwise, there are many libraries you could use to do this as well. Personally I like using Day.JS. Their format function it would look something like this and should fit your needs and more if you wanted to take that route.

dayjs(yourDateHere).format('MM-DD-YYYY')

cheers!

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