我有一个查询,我需要获取特定日期前后一天的事件。我需要在 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/