Created
June 7, 2013 15:46
-
-
Save jasonbartz/5730246 to your computer and use it in GitHub Desktop.
Query mongodb by date via CLI, so I don't forget. Via http://stackoverflow.com/questions/8749971/can-i-query-mongodb-objectid-by-date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function objectIdWithTimestamp(timestamp) { | |
// Convert string date to Date object (otherwise assume timestamp is a date) | |
if (typeof(timestamp) == 'string') { | |
timestamp = new Date(timestamp); | |
} | |
// Convert date object to hex seconds since Unix epoch | |
var hexSeconds = Math.floor(timestamp/1000).toString(16); | |
// Create an ObjectId with that hex timestamp | |
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000"); | |
return constructedObjectId | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Find all documents created after midnight on May 25th, 1980