最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

nodejs和mongodb .find命令

IT培训 admin 12浏览 0评论

nodejs和mongodb .find命令

我对mongodb /节点开发非常陌生,我有一个简短的问题。

通常,如果我要从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
  }
回答如下:

我听不懂results that are from the current day so from 12am to the current time,我打算给您摘录,但我不确定您要如何过滤日期。我会为您写一些选择。

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/docs/api.html#query_Query-sort

nodejs和mongodb .find命令

我对mongodb /节点开发非常陌生,我有一个简短的问题。

通常,如果我要从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
  }
回答如下:

我听不懂results that are from the current day so from 12am to the current time,我打算给您摘录,但我不确定您要如何过滤日期。我会为您写一些选择。

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/docs/api.html#query_Query-sort

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论