1

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.

Lion789
  • 4,402
  • 12
  • 58
  • 96
  • here is how you can do the reverse of what you asking. you can use the same method. http://stackoverflow.com/questions/15473772/how-to-convert-from-string-to-date-data-type – wathmal Jun 22 '16 at 03:26
  • uThanks for the example but how do I include that in the way I do my query without going through the shell... I am trying to solve it in that function as you can see above... can you give me an example with my function – Lion789 Jun 22 '16 at 03:33

0 Answers0