sorting - How can i sort addtoset grouped results mongoDB query -
currently have following:
db.questionanswers.aggregate( [ { $group : { _id : {sessionid: "$sessionid", quizid: "$quizid" }, sessionid: {$first: "$sessionid"}, quizid: { $first: "$quizid"}, sessionstarted: {$first: "$created"}, questionsubmitted: {$last: "$created"}, answer: {$addtoset: "$answer"} } }, ] )
after trying many combinations of $sort, $each, $push, $unwind, $orderby, i'm unable, @ point, sort answer
field, answers still being still in $group
s defined.
aggregating :
{ "_id" : objectid("xxx"), "quizid" : "xxx", "sessionid" : "xxx", "questionid" : "q1", "answer" : "answer a", "created" : isodate("2015-12-xxx"), "__v" : 0 }, { "_id" : objectid("xxx"), "quizid" : "xxx", "sessionid" : "xxx", "questionid" : "q2", "answer" : "answer b", "created" : isodate("2015-12-xxx"), "__v" : 0 }, // etc //
using mongodb's $out
output:
{ "_id" : { "quizid" : "xxx", "sessionid" : "xxx", }, "sessionstarted" : "2015-12-xxx", "questionssubmitted" : "2015-12-xxx" "answer" : { "0" : "answer a", "1" : "answer b" // etc // } }
this similar answer, although useful, doesn't offer of idea in terms of doing grouping.
Comments
Post a Comment