Mongoose Virtuals in MongoDB Aggregate

后端 未结 3 1066
有刺的猬
有刺的猬 2021-01-12 08:12

My Mongoose Schema is as follows:

var DSchema = new mongoose.Schema({
  original_y: {type: Number},,
  new_y: {type: Number},,
  date: {type: Date},
  dummy         


        
3条回答
  •  灰色年华
    2021-01-12 08:41

    the : <1 or true> form is used to include an existing field which is not the case here since the dateformatted field doesn't exist and you have to create it using an expression, $dateToString can be used:

    "$project": {
      "original_y": 1, 
      "new_y": 1,
      "dateformatted": { "$dateToString": { "format": "%Y-%m-%d %H:%M:%S", "date": "$date" } },
      ...
    

    Another option is to use it with $addFields:

    {
      "$project": {
        ...
      }
    },
    {
      "$addFields": {
        "dateformatted": { "$dateToString": {"format": "%Y-%m-%d %H:%M:%S", "date": "$date"} }
      }
    },
    ...
    

提交回复
热议问题