MongoDb: Convert date string to specific format in mongodb 3.6

前端 未结 3 1346
北海茫月
北海茫月 2021-01-25 03:11

I need to parse date value to specific format without using format field in dateFromString operator.

Mongo Playground

Current situation : in Mongodb 4.0 if I f

3条回答
  •  春和景丽
    2021-01-25 03:24

    This worked for me: I used combined solution from @ambienBeing and @techstack. Thanks

    Mongo playground

      "parsedDate": {
              "$dateFromParts": {
                "year": {
                  "$year": {
                    $dateFromString: {
                      dateString: {
                        "$concat": [
                          "$_id.dateHour",
                          ":00:00Z"
                        ]
                      }
                    }
                  }
                },
                "month": {
                  "$month": {
                    $dateFromString: {
                      dateString: {
                        "$concat": [
                          "$_id.dateHour",
                          ":00:00Z"
                        ]
                      }
                    }
                  }
                },
                "day": {
                  "$dayOfMonth": {
                    $dateFromString: {
                      dateString: {
                        "$concat": [
                          "$_id.dateHour",
                          ":00:00Z"
                        ]
                      }
                    }
                  }
                },
                "hour": {
                  "$hour": {
                    $dateFromString: {
                      dateString: {
                        "$concat": [
                          "$_id.dateHour",
                          ":00:00Z"
                        ]
                      }
                    }
                  }
                }
              }
            },
    

提交回复
热议问题