Fip
Fip

Reputation: 563

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.

Upvotes: 4

Views: 4271

Answers (1)

charlycou
charlycou

Reputation: 2000

You can achieve that using Aggregation and ProjectOperation.

ProjectionOperation projectionOperation = project().andExclude("user.password"); 
mongoTemplate.aggregate(Aggregation.newAggregation(projectionOperation);

Upvotes: 9

Related Questions