【问题标题】:$gte and $lte in mongoose with multiple condition具有多种条件的猫鼬中的 $gte 和 $lte
【发布时间】:2017-02-17 19:57:22
【问题描述】:

我是否在其他情况下错误地使用了$gte$lte?我得到了空数组,但是当我只做user_email:req.bodu.user_email 时,我能够获取所有数据。

function firstDayOfMonth() {
  var d = new Date(Date.apply(null, arguments));
  d.setDate(1);
  return d.toISOString();
}

function tomorrowDate() {
  var d = new Date();
  d.setDate(d.getDate() + 1)
  return d.toISOString();
}

Users.find({
    "user_email": req.body.user_email,
    "createdAt": {
      "$gte": firstDayOfMonth(),
      "$lte": tomorrowDate()
    }
  },
  function(err, response) {
    if (!err) {
      console.log(response)
      res.json(response);
    }
  });

【问题讨论】:

  • 您的firstDayOfMonth 值是多少,因为您的Date.now() 值是时间戳而不是日期格式?
  • @abdul, toISOString?
  • 这取决于你,在两种条件下提供相同的格式,它会起作用
  • 你能展示你的两个函数的结果吗?
  • 2016-10-01T06:28:37.000Z 2016-10-10T06:28:37.146Z

标签: javascript node.js mongodb mongoose


【解决方案1】:

它应该可以工作

我刚刚使用这些文档进行了测试

/* 1 */
{
    "_id" : ObjectId("57f9cc3dc4ac279a7c539d0f"),
    "TechActivityId" : 0,
    "LicenseId" : 0,
    "created" : "2016-01-01T00:00:00.000Z",
    "StateofActivity" : "State of Activity",
    "TechGId" : "123121134"
}

/* 2 */
{
    "_id" : ObjectId("57f9e674c4ac279a7c539d10"),
    "TechActivityId" : 0,
    "LicenseId" : 0,
    "created" : "2016-10-10T06:28:37.146Z",
    "StateofActivity" : "State of Activity",
    "TechGId" : "1231234"
}

db.getCollection('hola').find({"created":{"$gte":"2016-01-01T00:00:00.000Z","$lte":"2016-10-10T06:28:37.146Z"}})

我得到了两个文件

【讨论】:

  • 我打错了哈哈哈
  • 只是想指出,如果你想得到两个日期范围内的日期,你可以做 $gte 和 $lt。
猜你喜欢
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多