Saat Anda $unwind
reviews
bidang, struktur json pengembalian kueri tidak cocok dengan Hotel
Anda kelas lagi. Karena $unwind
operasi membuat reviews
sub objek alih-alih daftar. Jika Anda mencoba kueri Anda di robomongo atau alat lain, Anda dapat melihat objek yang dikembalikan seperti itu
{
"_id" : ObjectId("59b519d72f9e340bcc830cb3"),
"id" : "59b23c39c70ff63135f76b14",
"name" : "Signature",
"reviews" : {
"id" : 1,
"userName" : "Salman",
"rating" : 8,
"approved" : true
}
}
Jadi Anda harus menggunakan kelas lain daripada Hotel
seperti 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);