【发布时间】:2016-09-12 22:53:31
【问题描述】:
我正在尝试使用 MongoTemplate 在 Spring Data 中实现以下工作 mongoDb 查询:
db.answers.aggregate([
{ "$match" : { "object_id" : "1" } },
{ "$project": { 'answer_list': 1, 'profile': { $filter : { input: '$answer_list', as: 'answer', cond: { $eq: [ '$$answer.question', 2 ] } } } } },
{ "$unwind" : "$profile"},
{ "$unwind" : "$answer_list"},
{ "$group" : { "_id" : { "question" : "$answer_list.question", "answer" : "$answer_list.answer", "criteria" : "$profile.answer"}, "count" : { "$sum" : 1 } } },
{ "$sort" : { "_id.question" : 1, "_id.answer" : 1 } }
]);
该集合具有以下结构:
{
"_id" : ObjectId("..."),
"object_id" : ObjectId("..."),
"answer_list" : [
{
"question" : NumberLong(0),
"answer" : NumberLong(0)
},
{
"question" : NumberLong(1),
"answer" : NumberLong(2)
},
{
"question" : NumberLong(2),
"answer" : NumberLong(2)
}
]}
我在这里要做的是一份关于简单调查提交数据的报告。问题是“对第一个问题回答 0 的用户如何回答第二个问题?” 我花了一整天的时间搜索 SpringData Mongo Db 文档,但一无所获。 有人可以帮忙吗?
TIA
【问题讨论】:
-
我创建了DATAMONGO-1491 以添加对
$filter的支持。 -
谢谢克里斯托夫。我希望在实施之前找到解决方法
标签: java spring mongodb spring-data-mongodb