I am currently using nodejs with mongoose to update mongodb documents. However, I am trying to change the fields from a Date type to a String type. I figured it would be something easy but this obviously is not working...
function loopThroughDocs(docs) {
var x = 1;
docs.forEach(function(i) {
//sendNewDate and id for update in db
var dateFormat = formatDate(i.releaseDate);
updateProjWithStringDate(i, dateFormat);
x++;
});
if(x > docs.length) {
console.log("Finished going through docs");
}
}
function formatDate(theDate) {
var dateObj = new Date(theDate);
var month = dateObj.getUTCMonth(); //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var monthsName = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
if(year < 2016) {
console.log("Date is blank _____________");
return "";
} else {
console.log("Date is originally " + theDate);
return monthsName[month] + " " + day + ", " + year;
}
}
function updateProjWithStringDate(proj, stringDate) {
Project.update({_id: proj._id}, {releaseDate: stringDate}, function(err, res) {
if(err) {
console.log("Error " + err);
} else {
console.log("Updated Date for " + proj.title + " the date is " + stringDate);
}
});
}
The data looks like this and does not change to the string value:
"releaseDate": {
"$date": "2017-01-01T05:00:00.000Z"
},
Now I am trying to figure out how to change the date to string... One thing is that I need the original date as I am using it to create the string and after I create the string I can replace the date with the string.