Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude nested fields in Spring Data MongoDB

is it possible to exclude fields of nested Documents in Spring Data MongoDB?.

If tried it with

query.fields().exclude("user.password");

but that ends with the hole user document be excluded.

like image 489
Fip Avatar asked Oct 15 '25 07:10

Fip


1 Answers

You can achieve that using Aggregation and ProjectOperation.

ProjectionOperation projectionOperation = project().andExclude("user.password"); 
mongoTemplate.aggregate(Aggregation.newAggregation(projectionOperation);
like image 124
charlycou Avatar answered Oct 17 '25 01:10

charlycou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!