【问题标题】:RoboMongo gte date queryRoboMongo gte 日期查询
【发布时间】:2020-04-27 22:21:10
【问题描述】:

为什么 RoboMongo 1.0 会返回以下对象:

{
"code" : "bar",
"value" : 133.63,
"at" : "2017-05-03T10:42:08.000+1000"
"_id" : ObjectId("590927605105bf499025c202")
}

来自以下查询:

db.getCollection('foo').find({ "at" : 
 { $gte : new ISODate("2017-05-03T10:45:00.000+1000")}
})

?

我正在尝试获取所有记录上午 10:45 之后

【问题讨论】:

  • 是否将at 存储为date?还是只是string
  • 作为时间戳"at" : Timestamp(1493772128, 10),

标签: mongodb robo3t


【解决方案1】:

您可以使用 robo3t 客户端进行查询,例如:

db.getCollection('foo').find({"at": {$gte: new ISODate("2020-04-26")}}).sort({"_id":-1})

sort({"_id":-1}) 用于按降序排序以获取最新插入的项目。

【讨论】:

    【解决方案2】:

    Moment library 帮助更改日期并制作您想要的任何日期

    您可以通过以下方式要求 npm 模块:-

    var moment = require('moment');
    var fromDate = moment().set('hour', 10).set('minute', 45).set('second', 0).set('millisecond', 0).toDate();
    

    如果想设置特定的 UTC 日期,您可以简单地输入 moment("DateYouWant")

    查询将是:-

    db.getCollection('foo').find({ "at" : 
     { $gte : fromDate }
    })
    

    谢谢

    【讨论】:

    • 假设填充 Collection 的代码是 NodeJS,这是怎么回事?此外,我已经说过基准是Timestamp
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 2016-11-17
    • 2013-06-07
    • 1970-01-01
    • 2015-08-23
    • 2011-07-11
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多