I'm trying to implement in Spring Data using MongoTemplate the following working mongoDb query:
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 } }
]);
The collection has this structure:
{
"_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)
}
]}
What I'm trying to do here is a report on a simple survey submission data. The question is "How did the users that answered 0 to the first question answer to the second question?" I spent all day searching the SpringData Mongo Db docs but I found nothing. Can anyone help?
TIA
You can workaround this issue by providing your own AggregationExpression
.
ProjectionOperation agg = Aggregation.project() //
.and(new AggregationExpression() {
@Override
public DBObject toDbObject(AggregationOperationContext context) {
DBObject filterExpression = new BasicDBObject();
filterExpression.put("input", "$answer_list");
filterExpression.put("as", "answer");
filterExpression.put("cond", new BasicDBObject("$eq2", Arrays.<Object> asList("$$answer.question", 2)));
return new BasicDBObject("$filter", filterExpression);
}
}).as("profile");