Mongoose data formatting

const Article = new Schema({
    Title: { type: String, required: true }, 
    Content: { type: String, required: true },
    CreatedAt: { type: Date, default: new Date, get: v => moment(v).format("YYYY-MM-DD HH:mm")},
    UpdatedAt: { type: Date, default: new Date, get: v => moment(v).format("YYYY-MM-DD HH:mm")},
})

ArticleModel.findOne({ _id: ArticleId }, "Title Content CreatedAt")

now the returned CreatedAt is 2018-07-26T06:21:23.789Z without formatting. If you set getters:true :

Article.set("toJSON", { getters: true })

at this time, CreatedAt is" 2018-07-26 14 code 21, but the result has more fields UpdatedAt , so is there any way to query the method that the result is directly (_ id) Title Content CreatedAt field and the time has been formatted in it

)?
Mar.30,2021

deal with it with moment.js

http://momentjs.com


what if you want to query list

Menu