> db.tb_time_test.find() { "_id" : ObjectId("64c9cc1ffb4c9e5dcd00cfa2"), "id" : 1, "time" : ISODate("2023-08-02T11:23:11Z") } { "_id" : ObjectId("64c9cc83fb0a738bc82bdbaf"), "id" : 2, "time" : ISODate("2023-08-02T11:24:51Z") } { "_id" : ObjectId("64c9cc9515fdee4600c521d7"), "id" : 3, "time" : ISODate("2023-08-02T11:25:09Z") } { "_id" : ObjectId("64c9cc9b93b1f8be3997f08d"), "id" : 4, "time" : ISODate("2023-08-02T11:25:15Z") } { "_id" : ObjectId("64c9cc9ffe65999296081ba6"), "id" : 5, "time" : ISODate("2023-08-02T11:25:19Z") }
当前是isodate的日期格式,下面转换成字符串格式,我这里只到日期
> db.tb_time_test.aggregate([ {$project: { item: 1, date: { $dateToString: { format: "%Y-%m-%d", date: "$time"}}}}]) { "_id" : ObjectId("64c9cc1ffb4c9e5dcd00cfa2"), "date" : "2023-08-02" } { "_id" : ObjectId("64c9cc83fb0a738bc82bdbaf"), "date" : "2023-08-02" } { "_id" : ObjectId("64c9cc9515fdee4600c521d7"), "date" : "2023-08-02" } { "_id" : ObjectId("64c9cc9b93b1f8be3997f08d"), "date" : "2023-08-02" } { "_id" : ObjectId("64c9cc9ffe65999296081ba6"), "date" : "2023-08-02" }
标签:转换成,ObjectId,08,time,ISODate,2023,字符串,id From: https://www.cnblogs.com/hxlasky/p/17600272.html