How to convert epoch seconds to integer in mongodb

  • 1
  • Idea
  • Updated 4 years ago
I import hurricane reports (api: 'currenthurricane') to MongoDB and then run queries on them. I need for the numerous epoch timestamps to be integers instead of strings for certain date calculations. So I wrote this MongoDB query to make all of those conversions. It works well for me, so I am posting it here. Feedback welcome :)

db.storms.find().forEach(

function(doc) {
doc.currenthurricane.forEach(function(cHurr) {
cHurr.Current.Time.epoch = parseInt(cHurr.Current.Time.epoch);
cHurr.Current.TimeGMT.epoch = parseInt(cHurr.Current.TimeGMT.epoch);
cHurr.forecast.forEach(function(forecast) {
forecast.Time.epoch = parseInt(forecast.Time.epoch);
forecast.TimeGMT.epoch = parseInt(forecast.TimeGMT.epoch);
});
cHurr.ExtendedForecast.forEach(function(extfore) {
extfore.Time.epoch = parseInt(extfore.Time.epoch);
extfore.TimeGMT.epoch = parseInt(extfore.TimeGMT.epoch);
});
cHurr.track.forEach(function(track) {
track.Time.epoch = parseInt(track.Time.epoch);
track.TimeGMT.epoch = parseInt(track.TimeGMT.epoch);
});
});
db.storms.save(doc);
}
);
Photo of CloudGawker

CloudGawker

  • 4 Posts
  • 0 Reply Likes

Posted 4 years ago

  • 1

Be the first to post a reply!