1

This query generate a object with the key/value inside works as i expect

db.empdetails.aggregate( [ { $project: { reportInformationMap: { valu1: 1 } } } ] );

So in spring data i do:

Aggregation.project("reportInformationMap.valu1")

Spring data generate something like this:

{ "aggregate" : "__collection__" , "pipeline" : [ { "$project" : {  "valu1" : "$reportInformationMap.valu1"}}

it work but gimme valu1 as root value. i need it as nest value of reportInformationMap.

1 Answer 1

2

You have to use the ProjectionOperation.ProjectionOperationBuilder.nested method for this (nested defines the nested field binding for the field):

Here is an example with the following input document:

{ "name" : { "first" : "John", "last" : "Doe" } }

The code:

ProjectionOperation projection = Aggregation.project().and("name").nested(Fields.fields("name.first"))

Aggregation agg = newAggregation(projection);
AggregationResults<Document> results = mongoTemplate.aggregate(agg, "test", Document.class);
results.forEach(doc -> System.out.println(doc.toJson()));

The output (projected):

{ "name": {"first": "John"} }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.