1

i want to get the trunc date with such format :

20190326

this code in my schema :

 dateCreated: {
     
        type: Date,
        default: Date.now
    }
gives the following result :

2019-03-26T01:05:43.778Z

2 Answers2

0

Use Aggregation $dateToString

db.collection.aggregate([{
  $project: {
    date: {
      $dateToString: {
        format: "%d%m%Y",
        date: "$dateCreated"
      }
    }
  }
}])

You can set formate according to requirement. Sample data here.

Ashok
  • 2,846
  • 1
  • 12
  • 20
0

this solved it :

const date = new Date();
const today = date.getUTCFullYear().toString()+ (date.getUTCMonth()+1)+date.getUTCDate().toString();


export const taskSchema = new Schema ({
dateCreated: {
     
        type: String,
        default: today
    }
});