I'm looking for a solution to get the first entry that match my condition. Actually, I made an $unwind on the contribution collection, thne a $sort on the contributionDate and I get data like this :
"contributions" : {
"cuid" : "cjibozq4200aw0ltii23cqqdw",
"contributionDate" : ISODate("2018-06-12T12:58:31.530+0000"),
"hours" : 0,
"items" : 0,
"money" : 16.5,
"currency" : "chf",
"
},
"contributions" : {
"cuid" : "cdasd200lasikks",
"contributionDate" : ISODate("2019-06-12T12:58:31.530+0000"),
"hours" : 10,
"items" : 0,
"money" : 0,
"
}
Now my goal is to have the first time my user make a contribution with hours and with money. For that, I've got :
firstDonation : {
$first: {
$cond: [ { $and: [ { $gt: [ "$contributions.money", 0 ]}, { $not: "$contributions.rejectionDate" }] }, {
$dateToString: {
"date": "$contributions.contributionDate",
"format": "%d.%m.%Y",
onNull: '$noVal'
}
}, "" ]
}
},
firstVolunteering : {
$first: {
$cond: [ { $and: [ { $gt: [ "$contributions.items", 0 ]}, { }] }, {
$dateToString: {
"date": "$contributions.contributionDate",
"format": "%d.%m.%Y",
onNull: '$noVal'
}
}, "yes" ]}
},
But this is not working as expected. I didn't use the $cond the right way. My wish is to conditionally add to firstDonation the $first result that have $contributions.money > 0.