MongoDB日期转换

1、查询日期分组,把Long时间转换

 

db.getCollection('Warehouse.InoutstockDetail').aggregate([

    {

      $match:{"_id":ObjectId("59b89682e4b0bd561aa3554c")}   

    },

    {$project : {

         year : {$year : {$add:[new Date(0),"$createAt"]}},

         month : {$month : {$add:[new Date(0),"$createAt"]}},

         day:{$dayOfMonth:{$add:[new Date(0),"$createAt"]}}

    }},

    {$group : {

         _id : {year : "$year", month : "$month",day:"$day"},

         count : {$sum : 1}

    }}

])

 

代码展示: ProjectionOperation projectionOperation = Aggregation.project().andExpression("year(add(new java.util.Date(0),date))").as("year")

                .andExpression("month(add(new java.util.Date(0),date))").as("month")

                .andExpression("dayOfMonth(add(new java.util.Date(0),date))").as("day").andExclude("_id");

 

2、查询日期分组,日期类型为DATE

db.getCollection('Warehouse.InoutstockDetail').aggregate([

    {

      $match:{"_id":ObjectId("59b89682e4b0bd561aa3554c")}   

    },

    {$project : {

         year : {$year : "$createDate"},

         month : {$month : "$createDate"},

         day:{$dayOfMonth:"$createDate"}

    }},

    {$group : {

         _id : {year : "$year", month : "$month",day:"$day"},

         count : {$sum : 1}

    }}

])

你可能感兴趣的:(MongoDB日期转换)