Spring-data-mongo unable to instantiate java.util.List using Constructor
When you $unwind
reviews
field, query's return json structure does not match with your Hotel
class anymore. Because $unwind
operation makes reviews
a sub object instead of a list. If you try your query in robomongo or some other tools, you can see your return object is like that
{
"_id" : ObjectId("59b519d72f9e340bcc830cb3"),
"id" : "59b23c39c70ff63135f76b14",
"name" : "Signature",
"reviews" : {
"id" : 1,
"userName" : "Salman",
"rating" : 8,
"approved" : true
}
}
So you should use another class instead of Hotel
like UnwindedHotel
public class UnwindedHotel {
private String name;
private int pricePerNight;
private Address address;
private Review reviews;
}
UnwindOperation unwindOperation = Aggregation.unwind("reviews");
Aggregation aggregation = Aggregation.newAggregation(unwindOperation);
AggregationResults<UnwindedHotel> results=mongoOperations.aggregate(aggregation,"hotel", UnwindedHotel.class);