HiveBrain v1.2.0
Get Started
← Back to all entries
patternCritical

Date range query for past 24 hour in Mongo shell

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rangequeryhourmongodatepastshellfor

Problem

I am setting a cron job to collect results from MongoDB database profiler. I'd like to collect results within a 24 hrs period. I plan to run mongo command with javascript.

Question is, in Mongo shell, how do I write a query to find a date range from 24 hrs ago? Such as:

db.system.profile.find({
    "timestamp" : {
        $lte : ,
        $gt : 
    }
})

Solution

Found answer here:
https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript

db.system.profile.find({ 
  "timestamp" : { 
    $lt: new Date(), 
    $gte: new Date(new Date().setDate(new Date().getDate()-1))
  }   
})

Code Snippets

db.system.profile.find({ 
  "timestamp" : { 
    $lt: new Date(), 
    $gte: new Date(new Date().setDate(new Date().getDate()-1))
  }   
})

Context

StackExchange Database Administrators Q#112179, answer score: 50

Revisions (0)

No revisions yet.