Data type conversion in MongoDB

后端 未结 5 1257
离开以前
离开以前 2020-12-29 04:16

I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of docu

5条回答
  •  暖寄归人
    2020-12-29 04:29

    To convert date format to "xxxx-xx-xx"

    db.getCollection('analytics').aggregate([
     {$project: {
         day: {$dayOfMonth: "$createdAt"},
         month: {$month: "$createdAt"},
         year: {$year: "$createdAt"},
         }
      },
      {$project: {
          day: {$concat:["0", {$substr:["$day",0, 2]}]},
         month: {$concat:["0", {$substr:["$month",0, 2]}]},
         year: {$substr:["$year",0, 4]},
          }
      },
      {$project: {date: {$concat: [{$substr:["$year",0, 4]},"-", {$substr:["$month",0, 2]},"-", {$substr:["$day",0, 2]}]}}},
    ]);
    

提交回复
热议问题