Spring Data - MongoDb $ ifNull Aggregation

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

How can I do the aforementioned mango aggregation with Spring data?

+5
source share
1 answer

I know this is an old post, and you could find the answer, but, just for the sake of others, I post 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") ); 
0
source

All Articles