Sample Doc :
{
bioupdate: [
{
date: "02/03/2020",
ts: "1583133621197-02/03/2020_15:20:21",
status: "1"
},
{
date: "02/03/2020",
ts: "1583135570542-02/03/2020_15:52:50",
status: "1"
},
{
date: "02/03/2020",
ts: "1583135586272-02/03/2020_15:53:06",
status: "0"
},
{
date: "21-03-2020:17:35:08",
ts: 1584783308231,
status: "1"
}
]
}
Below is the code I've tried with aggregation pipeline splitting the string with first '-' and take the first element which is epoch timestamp and save it to the same field to an existing array.
db.novelmodel.aggregate([
{$match: {pin: "JAIN"}},
{
$project: {
pin: 1,
bioupdate: {
$filter: {
input: "$bioupdate",
as: "bioupdateArray",
cond: { $and: [
{$arrayElemAt:[{$split:["$$bioupdateArray.ts", "-"]}, 0]}
] }
}
}
}
},
{$out:"novelmodel"}
]);
It gives me an error message: "errmsg" : "$split requires an expression that evaluates to a string as a first argument, found: double".I'm not sure how filter to take only the date which has delimiter '-' in a string
$mapaggregation operator instead of the$filter.$mapallows you replace the existing string value with the transformed value for all elements of the array.