admin管理员组文章数量:1026989
I am trying to get the Mongo database to return all of the records created within the past week.
Each record has a field 'created at'
which will be something like '6/22/17 09:14'
. How do I check if this date occurred in the past week?
I have some code that looks like this:
the_collection.find({ALERT_CREATED : {> : new Date() - ONE_WEEK}}).toArray(function(error, results) {
if (error) {
callback(error);
} else {
callback(null, results);
}
});
With the different date formats, the two dates can't be pared.
UPDATE:
To clarify the issue, the dates in the database have the form 6/22/17 09:14
and as such cannot be pared correctly against a Date() object. Is there any way to pare them correctly?
I am trying to get the Mongo database to return all of the records created within the past week.
Each record has a field 'created at'
which will be something like '6/22/17 09:14'
. How do I check if this date occurred in the past week?
I have some code that looks like this:
the_collection.find({ALERT_CREATED : {> : new Date() - ONE_WEEK}}).toArray(function(error, results) {
if (error) {
callback(error);
} else {
callback(null, results);
}
});
With the different date formats, the two dates can't be pared.
UPDATE:
To clarify the issue, the dates in the database have the form 6/22/17 09:14
and as such cannot be pared correctly against a Date() object. Is there any way to pare them correctly?
- What is the field type? – Chava Geldzahler Commented Jun 22, 2017 at 23:21
- 1 I would strongly suggest storing dates as Date type, so you can perform date parisons with them. – Chava Geldzahler Commented Jun 22, 2017 at 23:44
3 Answers
Reset to default 2You may need to subtract days in MongoDB query. Please try below code:
the_collection.find(
{
"ALERT_CREATED":
{
$gte: (new Date((new Date()).getTime() - (7 * 24 * 60 * 60 * 1000)))
}
})
1 day = 1*24*60*60000 = 1 x 24 hours x 60 minutes x 60 seconds x 1000 milliseconds
First you need the date to pare the created_at
field to (today - 7), in MongoDB's date format. Then get only dates that e after.
var lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() -7);
db.the_collection.find({'created_at': {'$gte': lastWeek}});
The date does not seem to be a standardized format. You would have to manually convert the given string into a valid date string.
One way is splitting up your string and using dateFormParts
as documented here to construct a date.
{
$dateFromParts : {
'year': <year>, 'month': <month>, 'day': <day>,
'hour': <hour>, 'minute': <minute>, 'second': <second>,
'milliseconds': <ms>, 'timezone': <tzExpression>
}
}
I am trying to get the Mongo database to return all of the records created within the past week.
Each record has a field 'created at'
which will be something like '6/22/17 09:14'
. How do I check if this date occurred in the past week?
I have some code that looks like this:
the_collection.find({ALERT_CREATED : {> : new Date() - ONE_WEEK}}).toArray(function(error, results) {
if (error) {
callback(error);
} else {
callback(null, results);
}
});
With the different date formats, the two dates can't be pared.
UPDATE:
To clarify the issue, the dates in the database have the form 6/22/17 09:14
and as such cannot be pared correctly against a Date() object. Is there any way to pare them correctly?
I am trying to get the Mongo database to return all of the records created within the past week.
Each record has a field 'created at'
which will be something like '6/22/17 09:14'
. How do I check if this date occurred in the past week?
I have some code that looks like this:
the_collection.find({ALERT_CREATED : {> : new Date() - ONE_WEEK}}).toArray(function(error, results) {
if (error) {
callback(error);
} else {
callback(null, results);
}
});
With the different date formats, the two dates can't be pared.
UPDATE:
To clarify the issue, the dates in the database have the form 6/22/17 09:14
and as such cannot be pared correctly against a Date() object. Is there any way to pare them correctly?
- What is the field type? – Chava Geldzahler Commented Jun 22, 2017 at 23:21
- 1 I would strongly suggest storing dates as Date type, so you can perform date parisons with them. – Chava Geldzahler Commented Jun 22, 2017 at 23:44
3 Answers
Reset to default 2You may need to subtract days in MongoDB query. Please try below code:
the_collection.find(
{
"ALERT_CREATED":
{
$gte: (new Date((new Date()).getTime() - (7 * 24 * 60 * 60 * 1000)))
}
})
1 day = 1*24*60*60000 = 1 x 24 hours x 60 minutes x 60 seconds x 1000 milliseconds
First you need the date to pare the created_at
field to (today - 7), in MongoDB's date format. Then get only dates that e after.
var lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() -7);
db.the_collection.find({'created_at': {'$gte': lastWeek}});
The date does not seem to be a standardized format. You would have to manually convert the given string into a valid date string.
One way is splitting up your string and using dateFormParts
as documented here to construct a date.
{
$dateFromParts : {
'year': <year>, 'month': <month>, 'day': <day>,
'hour': <hour>, 'minute': <minute>, 'second': <second>,
'milliseconds': <ms>, 'timezone': <tzExpression>
}
}
本文标签: javascriptHow to compare dates in Mongo queryStack Overflow
版权声明:本文标题:javascript - How to compare dates in Mongo query - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745594171a2158063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论