Reputation: 15844
Using spring-data-mongodb-1.5.4
and mongodb-driver-3.4.2
I've a class Hotel
public class Hotel {
private String name;
private int pricePerNight;
private Address address;
private List<Review> reviews;
//getter, setter, default constructor, parameterized constructor
Review
class :
public class Review {
private int rating;
private String description;
private User user;
private boolean isApproved;
//getter, setter, default constructor, parameterized constructor
When I am calling Aggregation.unwind("reviews");
it throws
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments
UnwindOperation unwindOperation = Aggregation.unwind("reviews");
Aggregation aggregation = Aggregation.newAggregation(unwindOperation);
AggregationResults<Hotel> results=mongoOperations.aggregate(aggregation,"hotel", Hotel.class);
I see this question but does't help me.
How to resolve this?
Upvotes: 8
Views: 20478
Reputation: 1
U dont have to clean the Db ,just rename the previous collection and create a new collection with the name mentioned in the properties/yml file on your project and save it in the mongoDb database.
then run your project and save the new data using the save Api and u can observe the changes from comparing both the collections.
Upvotes: 0
Reputation: 313
It occurs to me a very different problem making me crazy for long time a day ago and I think it could be very useful to share here even if it is not about unwind:
{"header":{"key":"first", "value": 4}}
{"header":[{"key":"first", "value": 4}] }
When I was working only with new documents all was running fine, but then making more general tests I continuously had crashes with this mongodb error about List because in my DB I had old documents without list but with simple objects I forgot to remove and which were in conflict with the new model wanting a list.
Simple had I to clean DB and starting tests from scratch.
Upvotes: 0
Reputation: 3144
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);
Upvotes: 13