Mongodb日期时间到字符串(yyyy/mm/dd)
我有以下mongodb输出。我希望我的datetime采用以下格式:yyyy/mm/dd,以便在以后的阶段进行比较。我尝试了很多东西,以下是我认为最适合的两个: 输出: 另一种尝试:Mongodb日期时间到字符串(yyyy/mm/dd),mongodb,datetime,Mongodb,Datetime,我有以下mongodb输出。我希望我的datetime采用以下格式:yyyy/mm/dd,以便在以后的阶段进行比较。我尝试了很多东西,以下是我认为最适合的两个: 输出: 另一种尝试: { "collection": "device_color", "query": { "device_id": {"$in": [587,579,1241,543]},
{
"collection": "device_color",
"query": {
"device_id": {"$in": [587,579,1241,543]},
"datetime": {
"$gte": {
"$humanTime": "2020-09-01"
}
}
},
"fields": {
"datetime": 1,
"device_id": 2,
"color": 3
},
"sort": [
{
"name": "device_id",
"direction": -1
},
{
"name": "datetime",
"direction": 1
}
],
"$addFields": {
"sentDateString": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$datetime"
}
}
}
}
我也试过这样的方法:
{
"collection": "device_color",
"query": {
"device_id": {"$in": [587,579,1241,543]},
"datetime": {
"$gte": {
"$humanTime": "2020-09-01"
}
}
},
"$project": {
"datetime": { "$dateToString": { "format": "%Y-%m-%d", "date": "$datetime" } },
"device_id": 2,
"color": 3
},
"sort": [
{
"name": "device_id",
"direction": -1
},
{
"name": "datetime",
"direction": 1
}
]
}
{
"collection": "device_color",
"query": {
"device_id": {"$in": [587,579,1241,543]},
"datetime": {
"$gte": {
"$humanTime": "2020-09-01"
}
}
},
"$project": {
"datetime": { "$dateToString": { "format": "%Y-%m-%d", "date": "$datetime" } },
"device_id": 2,
"color": 3
},
"sort": [
{
"name": "device_id",
"direction": -1
},
{
"name": "datetime",
"direction": 1
}
]
}