문제

I have a document with no "timeCreated" field. I would like to get all the entries from the last 30 minutes. Iv'e heard that the _id field contains the time created logic within it.

How can i do that?

thanks!

도움이 되었습니까?

해결책

getTimestamp() will give you the date/time from the ObjectId.

You can do the following

var oldest = new Date(new Date() - new Date(30 * 60000));
db.collection.find().sort({ _id: -1 }).forEach(function(item) {
  if (item._id.getTimestamp() > oldest){
     ...
  }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top