MongoDB sorting date string (mm/dd/yyyy)

后端 未结 2 474
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 08:52

I\'m storing date as string in \"mm/dd/yyyy\" format. I want to sort by this date field. I tried below query with few test data.

db.collection.find().sort({d         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 09:17

    Within MongoDB 3.6 you can now use $dateFromString (https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/)

    db.logmessages.aggregate( [ {
       $project: {
          date: {
             $dateFromString: {
                dateString: '$date'
             }
          }
       }
    }, { $sort: { date : 1} } ] )
    

提交回复
热议问题