I try to use aggregation pipeline to append/create new date based on the previous pipe value and save to a new collection.(see my pipeline below). However, the syntax is wrong and I got an error says
disallowed field type Date in object expression (at 'date') // date: new Date('$_id.year', '$_id.month', '$_id.day')
I wonder how I can new a date using my previous year, month, day value in mongo aggregation pipeline? Basically, after transform my ISODate to year, month, and day for grouping, I wanna transform them back to the ISODate format.
pipeline = [{
$match: {
event: 'sample_event',
}
}, {
$project: {
_id: false,
uuid: '$properties.distinct_id',
time: '$properties.time'
}
}, {
$group: {
_id: {
year: {
$year: '$time'
},
month: {
$month: '$time'
},
day: {
$dayOfMonth: '$time'
},
uuid: '$uuid'
}
}
}, {
$group: {
_id: {
year: '$_id.year',
month: '$_id.month',
day: '$_id.day'
},
value: { $sum: 1 }
}
}, {
$sort: {
'_id.year': 1,
'_id.month': 1,
'_id.day': 1
}
}, {
$project: {
_id: {
$concat: [
{ $substr: ['$_id.year', 0, 4] },
'-',
{
$cond: [
{ $lte: [ '$_id.month', 9 ] },
{ $concat: [
'0',
{ $substr: [ '$_id.month', 0, 2 ] }
]},
{ $substr: [ '$_id.month', 0, 2 ] }
]
},
'-',
{
$cond: [
{ $lte: [ '$_id.day', 9 ] },
{ $concat: [
'0',
{ $substr: [ '$_id.day', 0, 2 ] }
]},
{ $substr: [ '$_id.day', 0, 2 ] }
]
},
]
},
date: new Date('$_id.year', '$_id.month', '$_id.day'), // errorrrr
value: 1
}
}, {
$out: 'output_collection'
}];