我试图使用URL中包含的参数过滤存储在MongoDB中的一些数据.

我的问题是,当发送请求时,服务器崩溃,并显示以下错误:

can't convert from BSON type string to Date

我try 将变量类型更改为Number,希望它能解决问题,但结果相同.

下面是我用来向服务器发送请求的代码:

async filter(){

        month = this.$refs.month.value;
        year = this.$refs.year.value;

        if(month != "Month" && year != ""){
          const addressToFetch = "/api/budget/" + year + "/" + month;
          console.log("Fetching" + addressToFetch);
          await fetch(addressToFetch).then(response => response.json())
      .then(response => {
          this.expenses = response;
          console.log(response);
      });

        }
    }

此函数在单击一个HTML表单按钮时执行,并从两个表单输入中获取值.

下面是我用来处理服务器端请求的代码:

app.get("/api/budget/:year/:month", verify, async (req, res) => {
  const user = req.session.user;

  const year = Number(req.params.year);
  const month = Number(req.params.month);

  console.log("Got " + year + "/" + month);

  const expenses = db
    .collection("expenses")
    .find({
      "users.username": user.username,
      $expr: {
        $and: [
          { $eq: [{ $year: "$date" }, year] },
          { $eq: [{ $month: "$date" }, month] },
        ],
      },
    })
    .toArray();

  console.log(expenses);
  res.json(expenses);
});

出于演示目的,我还发布了存储在数据库中的文档:

[
    {
        "_id": "6571eb70320499f416931fd8",
        "id": 2,
        "date": "2021-09-06T00:00:00.000Z",
        "description": "boringday",
        "category": "transport",
        "buyer": "giacomo",
        "cost": 49,
        "users": [
            {
                "username": "paolo",
                "amount": 16.333333333333332
            },
            {
                "username": "giacomo",
                "amount": 16.333333333333332
            },
            {
                "username": "giacomo",
                "amount": 16.333333333333332
            }
        ]
    },
    {
        "_id": "6571eb70320499f416931fe8",
        "id": 18,
        "date": "2019-04-10T00:00:00.000Z",
        "description": "badday",
        "category": "school",
        "buyer": "giacomo",
        "cost": 329,
        "users": [
            {
                "username": "luca",
                "amount": 109.66666666666667
            },
            {
                "username": "luca",
                "amount": 109.66666666666667
            },
            {
                "username": "giacomo",
                "amount": 109.66666666666667
            }
        ]
    },
    {
        "_id": "6571eb70320499f416931feb",
        "id": 21,
        "date": "2021-11-14T00:00:00.000Z",
        "description": "boringexperience",
        "category": "school",
        "buyer": "giacomo",
        "cost": 299,
        "users": [
            {
                "username": "giorgia",
                "amount": 99.66666666666667
            },
            {
                "username": "giacomo",
                "amount": 99.66666666666667
            },
            {
                "username": "giacomo",
                "amount": 99.66666666666667
            }
        ]
    },
    {
        "_id": "6571eb70320499f416931fff",
        "id": 41,
        "date": "2020-04-09T00:00:00.000Z",
        "description": "badtool",
        "category": "sport",
        "buyer": "giacomo",
        "cost": 349,
        "users": [
            {
                "username": "franco",
                "amount": 116.33333333333333
            },
            {
                "username": "franco",
                "amount": 116.33333333333333
            },
            {
                "username": "giacomo",
                "amount": 116.33333333333333
            }
        ]
    }
]

例如,在提交表单时,值为year=2021, month=11,我只希望返回第三个对象.

有人能帮我解决这个问题吗?

提前感谢您的耐心等待!

推荐答案

您的日期是以字符串格式保存在数据库中的.您不能直接从字符串中提取$year$month,这就是您获得can't convert from BSON type string to Date的原因.在比较之前,您必须将其保存为Date类型,或者在查询时将字符串转换为Date类型(使用$toDate运算符

.find({
  "users.username": user.username,
  $expr: {
    $and: [
      { $eq: [{ $year: { $toDate: "$date" } }, year] },
      { $eq: [{ $month: { $toDate: "$date" } }, month] }
    ]
  }
})

playground

Javascript相关问答推荐

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

如何获取转换字节的所有8位?

从PWA中的内部存储读取文件

过滤对象数组并动态将属性放入新数组

useNavigation更改URL,但不呈现或显示组件

通过嵌套模型对象进行Mongoose搜索

按下同意按钮与 puppeteer 师

扫描qr code后出错whatter—web.js

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

在带有背景图像和圆形的div中添加长方体阴影时的重影线

可更改语言的搜索栏

Html文件和客户端存储的相关问题,有没有可能?

覆盖TypeScrip中的Lit-Element子类的属性类型

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

每隔3个项目交替显示,然后每1个项目交替显示

FindByIdAndUpdate在嵌套对象中创建_id

找不到处于状态的联系人

带元素数组的Mongo聚合

Google OAuth 2.0库和跨域开放程序的问题-策略错误

如何从嵌套的json中获取最大值