【问题标题】:mongodb system current date (yyyy-mm-dd)mongodb系统当前日期(yyyy-mm-dd)
【发布时间】:2018-07-26 10:37:41
【问题描述】:

我只想从集合中提取当天的 MONGODB 数据。 我已经尝试过这个请求:

db.hello.aggregate([
    { $match: { "name": "btb" } },
    { $where: function() { 
        today = new Date(); 
        today.setHours(0,0,0,0);
        return (this.date.getTimestamp() >= today)
    }},
    { $group: {
        _id: { articles: "$articles" },
        total: { $sum: 1 }
    }},
    { $project: {
        "articles": 1,
        "total": 1
    }},
    { $sort: { "total":-1 } }
]) 

但我收到该消息错误:

####################################################################

Unable to execute the selected commands

Mongo Server error (MongoCommandException): Command failed with error 16436: 'Unrecognized pipeline stage name: '$where'' on server localhost:27017. 

The full response is:
{ 
    "ok" : 0.0, 
    "errmsg" : "Unrecognized pipeline stage name: '$where'", 
    "code" : NumberInt(16436)
}

####################################################################

【问题讨论】:

  • 我想在 MONGODB 中提取当天的数据,比如今天:26-07-2018,我已经尝试过这个请求: 可以请多解释一下
  • @rayane-btb 您能否提供更多详细信息(例如示例文档和预期结果)?
  • 想要从 mongodb 获得字段日期与当前日期匹配的结果

标签: mongodb sysdate


【解决方案1】:

试试这个

db.collection.find({ createdAt: { $lte: new Date() , $gte: new Date(ISODate().getTime() - 1000 * 60 * 18)} })`

18 分钟前(从现在开始)

【讨论】:

    【解决方案2】:

    我通常会这样获取数据:

    db.mycollection.find(
     { "createdAt" : 
      {
       "$gte" : new Date("2013-10-01T00:00:00Z"),
       "$lt" : new Date("2013-10-02T00:00:00Z") }
     })
    

    $gte 大于 $lt 更少

    【讨论】:

    • 我终于使用了这个解决方案,它对我有用:{date_field : { "$gte": new Date(new Date().getTime()-(1000 * 60 * 18))}}
    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多