How can I project date string or object instead of date number field in mongodb find query?

3477 views mongodb
3

I have a date field that is javascript number date and I want to get the string or object of date (like new Date(date)) in find query projection instead of date field itself that is number.

//I want to get 
datetime:new Date(date)
//instead of 
db.myCollection.find({},{date:1})

thanks.

answered question

Could you show how does your date field look like in database.

1 Answer

5

You can use forEach or map function

db.yourCollectionName.find({}).map(function(doc) {
    return {
        d:new Date(doc.datetime)
        };
});

posted this

Have an answer?

JD

Please login first before posting an answer.