MongoDb: Convert date string to specific format in mongodb 3.6

前端 未结 3 1343
北海茫月
北海茫月 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"
                        ]
                      }
                    }
                  }
                }
              }
            },
    
    0 讨论(0)
  • 2021-01-25 03:30

    I think following query will solve your problem because MongoDB 3.6 supports dateFromString

    Input:

    [
      {
        "dateS": "2020-01-16T08"
      }
    ]
    

    Query:

    db.collection.aggregate([
      {
        "$project": {
          "year": {"$substr": ["$dateS",0,4]},
          "month": {"$substr": ["$dateS",5,2]},
          "day": {"$substr": ["$dateS",8,2]},
          "hour": {"$substr": ["$dateS",11,2]}
        }
      },
      {
        $addFields: {
          "isoString": {
            "$concat": ["$year","-","$month","-","$day","T","$hour",":00:00Z"]
          }
        }
      },
      {
        $addFields: {
          "newDate": {
            $dateFromString: {"dateString": "$isoString"}
          }
        }
      }
    ])
    

    Output:

    [
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "day": "16",
        "hour": "08",
        "isoString": "2020-01-16T08:00:00Z",
        "month": "01",
        "newDate": ISODate("2020-01-16T08:00:00Z"),
        "year": "2020"
      }
    ]
    
    0 讨论(0)
  • 2021-01-25 03:35

    If I get the requirement right, Try the following query which uses: $dateFromParts

    Input:

    [
      {
        "date": ISODate("2020-01-16T08:54:17.604Z")
      }
    ]
    

    Query:

    db.collection.aggregate([
      {
        $project: {
          outputDate: {
            $dateFromParts: {
              "year": {
                $year: "$date"
              },
              "month": {
                $month: "$date"
              },
              "day": {
                $dayOfMonth: "$date"
              },
              "hour": {
                $hour: "$date"
              }
            }
          }
        }
      }
    ]);
    

    O/P:

    [
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "outputDate": ISODate("2020-01-16T08:00:00Z")
      }
    ]
    

    Playground Test Link

    0 讨论(0)
提交回复
热议问题