【问题标题】:nodejs and mongodb .find commandnodejs 和 mongodb .find 命令
【发布时间】:2020-05-06 16:21:24
【问题描述】:

我对 mongodb / node 开发非常陌生,我有一个简单的问题。

通常,如果我从 SQL 中提取数据,我可以使用 WHERE 子句并缩小结果范围,但是对于 mongo,我不能 100% 确定我将如何去做。

目前我有一个响应获取请求的 api 及其工作,它只是拉整个表。

api.get('/', (req, res) => {
    F5.find({}, (err, f5data) => {
        if(err){
            res.send(err);
        }
        res.json(f5data);
    });
});

在 mongodb 内部,我有一个表,其中有一列“时间”,即日期类型。

我正在尝试使用 .find 命令过滤掉当天的结果,例如从上午 12 点到当前时间。

数据如下所示:

[
  {
    "_id": "5eaac0ff2e79d08740f6b1bc",
    "totalConnections": 327,
    "time": "2020-04-30T12:13:51.398Z",
    "__v": 0
  }

【问题讨论】:

  • 这能回答你的问题吗? Node.js to MongoDB: find by Date
  • 不幸的是,我认为该帖子是关于将日期存储为字符串然后将其转换为日期。
  • 您的问题是什么?是关于通过过滤日期字段或其他方式获取数据的查询吗?问题不清楚。
  • 是的,我也不明白这个问题,你要过滤的部分。

标签: node.js mongodb


【解决方案1】:

我不明白 results that are from the current day so from 12am to the current time 我打算给你一个 sn-p 但我不确定你想如何过滤日期。我会为你写几个选项。

F5.find(options)
  .sort({date: -1})
  .then((result) => {
    // do something with the result
  })
  .catch((e) => console.log(e));

在这里我将分享一些排序选项,您可以尝试测试其中的一些。

.sort('-date')
.sort({date: 'desc'})
.sort({date: 'descending'})
.sort([['date', -1]]

更多信息:https://mongoosejs.com/docs/api.html#query_Query-sort

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 2016-03-15
    • 1970-01-01
    • 2021-08-14
    • 2015-11-13
    • 1970-01-01
    • 2017-03-03
    相关资源
    最近更新 更多