mongodb - 在 MongoDB Shell 中向 ISODate 添加/减去天数

标签 mongodb shell

我有一个查询,我需要获取特定日期前后一天的事件。我需要在 ISODate 变量上加减一天。这是我的查询:

db.event.find().forEach( function (x) {

  print("x : " + x.EventID + ", " + x.ISODate); 
  db.events.find( {
   "$or" : [{
       "StartDate" : { "$gte" : x.ISODate } // Here i need to subtract one day
       }, {
           "EndDate": { "$lt" : x.ISODate} // Here i need to add one day
           }]
}).forEach(function(otherDay) {
        print("x.EventID : " + x.EventID + ", other.Date : " + otherDay.StartDate + " - " + otherDay.EndDate);
      });

});

如何在 mongodb shell 中为 ISODate 变量添加或减去天数?

最佳答案

此问题已在 Query to get last X minutes data with Mongodb 上得到解答

query = {
    timestamp: { // 18 minutes ago (from now)
        $gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
    }
}

在你的情况下,持续几天:

"StartDate" : { "$gte" : new Date(ISODate().getTime() - 1000 * 3600 * 24 * 3) }

"StartDate" : { "$gte" : new Date(ISODate().getTime() - 1000 * 86400 * 3) }

(这里 3 是您的天数)

关于mongodb - 在 MongoDB Shell 中向 ISODate 添加/减去天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996728/

相关文章:

javascript - 如何在 meteor 事件期间在单个 <li> 上添加类

python - 操纵列字段以获得清晰的表示

shell - get "ERROR: Can' t 从 ZooKeeper 获取主地址; znode data == null"使用 Hbase shell 时

javascript - Mongoose : Merging two documents that reference each other

bash - 使用函数中止 bash 脚本

linux - 每当我打开 gvim 时检查进程

linux - 如何遍历 .txt 并将每行的值用作变量 Linux Shell

javascript - Mongoose ,nodejs,全文搜索

php - Doctrine ODM/PHP/MongoDB - 无法查询引用的元素

node.js - 如何使用 nodejs、redis 和 mongodb 设计基于聊天的应用程序?