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 : {&gt : 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 : {&gt : 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?

Share Improve this question edited Jun 22, 2017 at 22:56 sumowrestler asked Jun 22, 2017 at 21:53 sumowrestlersumowrestler 3655 silver badges21 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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 : {&gt : 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 : {&gt : 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?

Share Improve this question edited Jun 22, 2017 at 22:56 sumowrestler asked Jun 22, 2017 at 21:53 sumowrestlersumowrestler 3655 silver badges21 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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