जब आप $unwind reviews फ़ील्ड, क्वेरी की वापसी json संरचना आपके Hotel . से मेल नहीं खाती है अब कक्षा। क्योंकि $unwind ऑपरेशन reviews makes करता है एक सूची के बजाय एक उप वस्तु। यदि आप अपनी क्वेरी को रोबोमोंगो या किसी अन्य टूल में आज़माते हैं, तो आप देख सकते हैं कि आपकी रिटर्न ऑब्जेक्ट इस तरह है
{
"_id" : ObjectId("59b519d72f9e340bcc830cb3"),
"id" : "59b23c39c70ff63135f76b14",
"name" : "Signature",
"reviews" : {
"id" : 1,
"userName" : "Salman",
"rating" : 8,
"approved" : true
}
}
तो आपको Hotel . की जगह दूसरी क्लास का इस्तेमाल करना चाहिए जैसे 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);