Vous pouvez le faire en vérifiant si la date est nulle ou manquante en utilisant $type
, s'il manque, supprimez-le en utilisant $$REMOVE
,
db.collection.aggregate([
{
$project: {
date1: {
$cond: {
if: {
$or: [
{ $eq: [{ $type: "$date1" }, "missing"] },
{ $eq: ["$date1", null] }
]
},
then: "$$REMOVE",
else: {
$dateFromString: {
dateString: {
$dateToString: {
format: "%Y-%m-%d",
date: "$date1"
}
}
}
}
}
},
date2: {
$cond: {
if: {
$or: [
{ $eq: [{ $type: "$date2" }, "missing"] },
{ $eq: ["$date2", null] }
]
},
then: "$$REMOVE",
else: {
$dateFromString: {
dateString: {
$dateToString: {
format: "%Y-%m-%d",
date: "$date2"
}
}
}
}
}
},
date3: {
$cond: {
if: {
$or: [
{ $eq: [{ $type: "$date3" }, "missing"] },
{ $eq: ["$date3", null] }
]
},
then: "$$REMOVE",
else: {
$dateFromString: {
dateString: {
$dateToString: {
format: "%Y-%m-%d",
date: "$date3"
}
}
}
}
}
}
}
}
])