java - Does Spring Data MongoDb support $filter array aggregations operator? -
i'm trying implement in spring data using mongotemplate 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 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 here report on simple survey submission data. question "how did users answered 0 first question answer second question?" spent day searching springdata mongo db docs found nothing. can help?
tia
you can workaround issue providing 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");
Comments
Post a Comment