У меня есть следующая структура документа для student_details и subject_details в mongodb.
student_details
================
{
"_id" : "STD-1",
"name" : "XYZ",
"subjects" : [
{
"$ref" : "subject_details",
"$id" : "SUB-1"
},
{
"$ref" : "subject_details",
"$id" : "SUB-2"
},
{
"$ref" : "subject_details",
"$id" : "SUB-3"
}
]
}
subject_details
===============
{
"_id" : "SUB-1",
"name" : "MATHEMATICS",
"status" : "ACTIVE"
}
{
"_id" : "SUB-2",
"name" : "PHYSICS",
"status" : "ACTIVE"
}
{
"_id" : "SUB-3",
"name" : "CHEMISTRY",
"status" : "INACTIVE"
}
Теперь, когда я запускаю приведенный ниже запрос с консоли mon go, он выдает мне правильный вывод.
Mon go query
db.student_details.aggregate([
{
$unwind: '$subjects'
},
{
$set: {
'fk': {
$arrayElemAt: [{
$objectToArray: '$subjects'
}, 1]
}
}
},
{
$lookup: {
'from': 'subject_details',
'localField': 'fk.v',
'foreignField': '_id',
'as': 'subject'
}
},
{
$match: {
'subject.status': 'ACTIVE'
}
},
{
$group: {
'_id': '$_id',
'name': {
$first: '$name'
},
'subjects': {
$push: {
$arrayElemAt: ['$subject', 0]
}
}
}
}
])
Вывод запроса
{
"_id" : "STD-1",
"name" : "XYZ",
"subjects" : [
{
"_id" : "SUB-1",
"name" : "MATHEMATICS",
"status" : "ACTIVE"
},
{
"_id" : "SUB-2",
"name" : "PHYSICS",
"status" : "ACTIVE"
}
]
}
Но когда я пытаюсь преобразовать его в Spring data mon go aggeragation framework, он выдает ошибку Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments ] with root cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
Репозиторий данных Spring
@Repository
public interface StudentDetailRepo extends MongoRepository<StudentDetail, String> {
@Aggregation(pipeline = {
" {" +
" $match: {" +
" 'name': 'XYZ'" +
" }" +
" }," +
" {" +
" $unwind: '$subjects'" +
" }," +
" {" +
" $set: {" +
" 'fk': {" +
" $arrayElemAt: [{" +
" $objectToArray: '$subjects'" +
" }, 1]" +
" }" +
" }" +
" }," +
" {" +
" $lookup: {" +
" 'from': 'subject_details'," +
" 'localField': 'fk.v'," +
" 'foreignField': '_id'," +
" 'as': 'subject'" +
" }" +
" }," +
" {" +
" $match: {" +
" 'subject.status': 'ACTIVE'" +
" }" +
" }," +
" {" +
" $group: {" +
" '_id': '$_id'," +
" 'name': {" +
" $first: '$name'" +
" }," +
" 'subjects': {" +
" $push: {" +
" $arrayElemAt: ['$subject', 0]" +
" }" +
" }" +
" }" +
" }"})
public List<StudentDetail> findStudentWithActiveSubjects();
}