Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data - MongoDb aggregation $ifNull

db.collection.aggregate([
    {$match : { name : "name" } },
    {$project: {
        name: 1,
        sent: { 
            $size: {
                "$ifNull": [ "$audience", [] ]
            } 
        }
    }
}]);

How can I do the above mongo aggregation with Spring data?

like image 757
Francesco Luigi Mirenda Avatar asked May 11 '16 13:05

Francesco Luigi Mirenda


1 Answers

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")
);
like image 75
UserAlpha Avatar answered Oct 10 '22 12:10

UserAlpha