Jalaleddin Hosseini
Jalaleddin Hosseini

Reputation: 2272

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

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.

Upvotes: 2

Views: 53

Answers (1)

Mohammad Nouraein
Mohammad Nouraein

Reputation: 163

You can use forEach or map function

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

Upvotes: 2

Related Questions