在 MongoDB Shell 中为 ISODate 添加/减去天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30996728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding/Subtracting days to ISODate in MongoDB Shell
提问by cuneytyvz
I have a query where I need to get events that are one day before or after from a specific date. I need to add or subtract one day to that ISODate variable. Here is my query :
我有一个查询,我需要获取特定日期之前或之后一天的事件。我需要在该 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);
});
});
How can i add or subtract days to an ISODate variable in mongodb shell?
如何在 mongodb shell 中为 ISODate 变量添加或减去天数?
回答by Constantin Guay
This has been answered on Query to get last X minutes data with Mongodb
这已在Query 上得到回答以使用 Mongodb 获取最后 X 分钟的数据
query = {
timestamp: { // 18 minutes ago (from now)
$gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
}
}
And in your case, for a number of days:
在你的情况下,几天:
"StartDate" : { "$gte" : new Date(ISODate().getTime() - 1000 * 3600 * 24 * 3) }
or
或者
"StartDate" : { "$gte" : new Date(ISODate().getTime() - 1000 * 86400 * 3) }
(here the 3is your number of days)
(这里的3是您的天数)
回答by cenk
Not an exact answer but related.
不是一个确切的答案,而是相关的。
I needed to increment a date field for all items in my MongoDB collection in place.
我需要为我的 MongoDB 集合中的所有项目增加一个日期字段。
The query below will add 1 dayto myDateFieldin myCollection.
查询下面将增加1天到myDateField在MyCollection的。
db.myCollection.find().snapshot().forEach(
function (elem) {
db.myCollection.update(
{
_id: elem._id
},
{
$set: {
myDateField: new Date(elem.myDateField.getTime() + 1*24*60*60000)
}
}
);
}
);
1 day = 1*24*60*60000 = 1 x 24 hours x 60 minutes x 60 seconds x 1000 milliseconds
- Use minus if you need to subtract / go back in time.
- 如果您需要减去/回到过去,请使用减号。
回答by Akshay Hazari
I feel your question was misunderstood.
我觉得你的问题被误解了。
I hope you needed to subtract a day and add a day in your query , which you could do like so: {$subtract : [x.ISODate,(1 * 24 * 60 * 60 * 1000)]}
我希望您需要在查询中减去一天并添加一天,您可以这样做: {$subtract : [x.ISODate,(1 * 24 * 60 * 60 * 1000)]}
db.event.find().forEach( function (x) {
print("x : " + x.EventID + ", " + x.ISODate);
db.events.find( {
"$or" : [{
"StartDate" : { "$gte" : {$subtract : [x.ISODate,(1 * 24 * 60 * 60 * 1000)]} } // Here i need to subtract one day
}, {
"EndDate": { "$lt" : {$subtract : [x.ISODate,(-1 * 24 * 60 * 60 * 1000)]}} // Here i need to add one day
}]
}).forEach(function(otherDay) {
print("x.EventID : " + x.EventID + ", other.Date : " + otherDay.StartDate + " - " + otherDay.EndDate);
});
});