I want to make a log analysis system based on mongodb. A single document is as follows:
{
"_id" : ObjectId("5c6a14c4421aa97a4714b291"),
"some":"data"
"startTime" : ISODate("2019-02-18T02:13:24.244Z"),
"endTime" : ISODate("2019-02-18T02:13:24.279Z")
}
because startTime is a millisecond time, I group to a second-based time, so I write the following code for conversion:
db.apiMonitor.aggregate([
{
$project:{
groupField:"$startTime"
}
},{
$project:{
groupField:{
$dateToString:{
format:"%Y-%m-%d %H:%M:%S",
date:"$groupField"
}
}
}
},{
$project:{
groupField:{
$dateFromString:{
format:"%Y-%m-%d %H:%M:%S",
dateString:"$groupField"
}
}
}
}
])
The process converts ISODate into a time string using $dateToString to remove millisecond information, and then reconverts it to ISODate via $dateFromString, but runs erratically as follows:
command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression "$dateFromString"",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
}
can you explain what caused this error, or is there any other solution?