4

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.

Fip
  • 503
  • 1
  • 5
  • 11

1 Answers1

9

You can achieve that using Aggregation and ProjectOperation.

ProjectionOperation projectionOperation = project().andExclude("user.password"); 
mongoTemplate.aggregate(Aggregation.newAggregation(projectionOperation);
charlycou
  • 1,778
  • 13
  • 37