db.collection.aggregate([
    {$match : { name : "name" } },
    {$project: {
        name: 1,
        sent: { 
            $size: {
                "$ifNull": [ "$audience", [] ]
            } 
        }
    }
}]);
How can I do the above mongo aggregation with Spring data?
I know this is an old post and you might have found the answer, but, just for the sake of others, I'm posting it here.
Aggregation aggregation = Aggregation.newAggregation(
.match(Criteria.where("name").is("name"))
.project("name")        
    .and(ArrayOperators.Size.lengthOfArray(ConditionalOperators.ifNull("audience").then(Collections.emptyList()))).as("sent")
);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With