我正在使用mongodb的spring数据,我在MongoDB中有一个c_user集合,如下所示:
db.c_user.find().pretty();
{
"_id" : "user-1",
"journeys" : [
{
"fromStation" : "A",
"toStation" : "B"
},
{
"fromStation" : "B",
"toStation" : "C"
},
{
"fromStation" : "A",
"toStation" : "D"
}
]
},
{
"_id" : "user-2",
"journeys" : [
{
"fromStation" : "A",
"toStation" : "B"
},
{
"fromStation" : "B",
"toStation" : "C"
},
{
"fromStation" : "D",
"toStation" : "E"
}
]
}
我想要的是获取匹配'journeys.fromStation'==“A”的c_user文档列表。因此,如果您在mongo shell中运行以下查询:
db.c_user.runCommand({
"aggregate" : "c_user" ,
"pipeline" : [ { "$match" : { "journeys.fromStation" : "A"}} ,
{ "$unwind": "$journeys"} ,
{ "$match" : { "journeys.fromStation" : "A"}}
]
});
我得到了c_user文档列表,这是我所期望的:
{
"_id" : "user-1",
"journeys" : [
{
"fromStation" : "A",
"toStation" : "B"
},
{
"fromStation" : "A",
"toStation" : "D"
}
]
},
{
"_id" : "user-2",
"journeys" : [
{
"fromStation" : "A",
"toStation" : "B"
}
]
}
}
现在我需要使用spring数据mongo聚合执行此操作:
TypedAggregation<CUser> aggregation = newAggregation(CUser.class,
match(where("journeys.fromStation").is("A")),
unwind("journeys"),
match(where("journeys.fromStation").is("A")));
AggregationResults<CUser> results = mongoTemplate.aggregate(aggregation, CUser.class);
return results.getMappedResults();
执行此操作时,我收到此错误:
org.springframework.data.mapping.model.MappingInstantiationException:
Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments
显然它是一个无法实例化的List.class。似乎返回的对象是BasicDbLIst。
无论如何,那么如何获得与mongo shell相同的结果呢?即作为List<CUser>
任何帮助表示赞赏...
此致 GM