Skip to content

Instantly share code, notes, and snippets.

@takeshy
Last active March 19, 2016 12:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshy/5126427 to your computer and use it in GitHub Desktop.
Save takeshy/5126427 to your computer and use it in GitHub Desktop.
現在の時間から指定された秒より前のISODateを作成して、Mongo Shellで検索しやすくする
function zp(num){
return(num < 10 ? "0"+num.toString() : num.toString());
}
function toISODate(d){
return ISODate(d.getUTCFullYear() + "-" + zp(d.getUTCMonth() + 1) + "-" + zp(d.getUTCDate()) + "T"+zp(d.getUTCHours()) + ":" + zp(d.getUTCMinutes()) + ":" + zp(d.getUTCSeconds()));
}
function before(n){
return toISODate(new Date(Date.now() - n * 1000));
}
//accessコレクションのTimeフィールドのDataが600秒(10分)以内のレコードを検索
//db.access.find({"time":{$gt: before(600)}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment