거인의 어깨위에 서려는
[MongoDB] ObjectId 활용법 (타임스탬프, Array넣기)
Jane Park
2016. 11. 3. 14:49
1. ObjectId로 날짜 확인하기
ObjectId는 타임스탬프를 가지고 있는데 shell에 정의된
getTimestamp() 함수를 사용하면 문서의 생성 날짜를 가져올 수 있다.
This file contains hidden or 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
ObjectId.prototype.getTimestamp = function() { | |
return new Date(parseInt(this.toString().slice(0,8), 16) * 1000); | |
} |
2. ObejctId를 Array로 저장할 때
ID를 String으로 저장하는 것보단 ObjectId 그대로 저장하는 것이 좋다.
객체는 12바이트인데 String으로 저장하면 24바이트가 되는 것도 하나의 이유이고, 무엇보다 드라이버에서 관리하기 용이하다.
참고문헌
http://stackoverflow.com/questions/7327296/how-do-i-extract-the-created-date-out-of-a-mongo-objectid