在猫鼬中使用ISODate的正确方法是什么 [英] What is the right way to work with ISODate in Mongoose

查看:140
本文介绍了在猫鼬中使用ISODate的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图用猫鼬调用mongodb集合,但在调用中使用日期却遇到困难

I am trying to call a mongodb collection with mongoose but I am having trouble using dates in the call

const siteReviews = await Review.countDocuments({
  'clientId': clientObj.ClientBrandID, 
  'siteSource': 'SomeSite', 
  'reviewDate':{
      $gt:"2018-12-24T18:04:47.806Z",
      $lt:"2019-04-03T17:04:47.806Z"
  }
})

我知道我在这两个日期之间有数据,但是我得到了0.我也曾尝试直接在代码中使用ISODate("),但它正在中断

I know for a fact that I have data between these two dates but i get 0 back.I have also tried to use ISODate("") directly in the code but it is breaking

推荐答案

实际上,您已经在架构中将reviewDate字段的type定义为Date,并且在此处将其作为String传递.

Actually, You have defined the type of the reviewDate field as Date in your schema and here you are passing it as String.

因此,基本上,您需要将String日期转换为Date对象,并且可以使用 moment

So basically you need to convert the String date to Date Object and can be easily done using moment library

const googleReviews = await Review.countDocuments({
  clientId: clientObj.ClientBrandID,
  siteSource: "SomeSite",
  reviewDate: {
    $gt: moment("2018-12-24T18:04:47.806Z").toDate(),
    $lt: moment("2019-04-03T17:04:47.806Z").toDate()
  }
});

这篇关于在猫鼬中使用ISODate的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆