Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得特定月份';在节点js中从mongodb获取数据?_Javascript_Mongodb_Momentjs - Fatal编程技术网

Javascript 如何获得特定月份';在节点js中从mongodb获取数据?

Javascript 如何获得特定月份';在节点js中从mongodb获取数据?,javascript,mongodb,momentjs,Javascript,Mongodb,Momentjs,我有一个用例,在其中我需要找到特定月份的数据。如何获取给定月份的开始和结束日期 下面是示例代码 {"_id":"5e00bc55c31ecc38d023b156","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T13:08:37.841Z"}, {"_id":"5e00bbd2c31ecc38d023b155","heat":20,"humidity":10,

我有一个用例,在其中我需要找到特定月份的数据。如何获取给定月份的开始和结束日期

下面是示例代码

{"_id":"5e00bc55c31ecc38d023b156","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T13:08:37.841Z"},
{"_id":"5e00bbd2c31ecc38d023b155","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T13:06:26.366Z"},
{"_id":"5df4a8fb46b9da1e2c0731df","heat":88,"humidity":80,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-14T09:18:51.892Z"},
{"_id":"5e00b50bc127260398cf51dd","heat":20,"humidity":10,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-23T12:37:31.127Z"},
{"_id":"5df20e44e7c51b4bd0095af3","heat":41,"humidity":26,"deviceId":"a-1","template":"13435158964","entryDayTime":"2019-12-12T09:54:12.375Z"}

这是我的代码,没有瞬间。js

有效载荷:

{
    "deviceId":"a-1",
    "year":2019,
    "month":"December"
}
{
    "deviceId":"a-1",
    "year":2019,
    "month":10
}
这些是我在控制台中得到的时间范围(在聚合函数中传递的时间)

使用矩.js编码

有效载荷:

{
    "deviceId":"a-1",
    "year":2019,
    "month":"December"
}
{
    "deviceId":"a-1",
    "year":2019,
    "month":10
}
我也尝试过使用moment.js。但是我没有像数据库的时间格式那样得到时间

Collection.aggregate([
  {
    $match: {
              "deviceId": payload.deviceId,
              "entryDayTime": {
                 $lt:moment([payload.year]).month(payload.month).startOf('month').tz('Asia/Kolkata').format(),
                 $gte:moment([payload.year]).month(payload.month).endOf('month').tz('Asia/Kolkata').format()
               }
            }
  }
])

下面是我在控制台中得到的时间戳

2019-11-01T00:00:00+05:30
2019-11-30T23:59:59+05:30
如果首选moment.js,如何更改与示例代码的时间格式类似的时间格式?

只需尝试以下代码:

var dated="2019-11-01T00:00:00+05:30";
var newdated= new Date(dated);
var output= newdated.toISOString();
console.log(output);
结果:

'2019-10-31T18:30:00.000Z'
toISOString()
方法返回简化扩展
ISO格式(ISO 8601)
的字符串,其长度始终为24或27个字符(
YYYY-MM-DDTHH:MM:ss.sssZ
±YYYYYY-MM-DDTHH:MM:ss.sssZ


时区始终为零UTC偏移量,由后缀“Z”表示。

要查找特定月份的数据,请使用构造函数创建范围:

const payload = {
    "deviceId": "a-1",
    "year": 2019,
    "month": 11 // months start from 0 = January, so 11 = December 
}

const from = new Date(Date.UTC(payload.year, payload.month, 1)).toISOString(); // "2019-12-01T00:00:00.000Z"
const to = new Date(Date.UTC(payload.year, payload.month + 1, 1)).toISOString(); // "2020-01-01T00:00:00.000Z"
然后按如下方式使用它们:

Collection.aggregate([
  {
    $match: {
       "deviceId": payload.deviceId,
       "entryDayTime": {
          $lt: to,
          $gte: from
       }
    }
  }
])

工作示例

您想要此格式的时间戳2019-12-23T13:08:37.841Z?是的。我想用这种形式我已经给出了答案,请检查。你为什么没有考虑我的答案???@Aravind为什么你根本没有考虑我的答案,我可以知道原因吗?不,我也试过你的答案。“你误会我了,@Prabhjot Singh Kainth。但我也有同样的问题。对不起,如果你感觉不好的话。但我确实先试过你的答案。问题是,我的数据库以以下格式存储时间戳,
2019-12-23T06:38:37.841-06:30