I have data collection like this:
> db.LogBuff.find() { "_id" : ObjectId("578899d5d2b76f77d083f16c"), "SUBJECT" : "DD", "SYS" : "A" } { "_id" : ObjectId("578899d5d2b76f77d083f16d"), "SUBJECT" : "AA", "SYS" : "B" } { "_id" : ObjectId("578899d5d2b76f77d083f16e"), "SUBJECT" : "BB", "SYS" : "A" } { "_id" : ObjectId("578899d5d2b76f77d083f16f"), "SUBJECT" : "AA", "SYS" : "C" } { "_id" : ObjectId("578899d5d2b76f77d083f170"), "SUBJECT" : "BB", "SYS" : "A" } { "_id" : ObjectId("578899d5d2b76f77d083f171"), "SUBJECT" : "BB", "SYS" : "A" } { "_id" : ObjectId("578899d5d2b76f77d083f172"), "SUBJECT" : "CC", "SYS" : "B" } And I want to extract output as below (with distinct "SYS" values)
{"SUBJECT" : "AA", "SYS" : ["A","B","C","D"]} {"SUBJECT" : "BB", "SYS" : ["A","B","C","D"]} {"SUBJECT" : "CC", "SYS" : ["A","B","C"]} Here is my code and I am stuck in the middle, please help me to sort this
db.LogBuff.aggregate([{ "$unwind": "$SYS" }, { "$group": { _id: { "_id": "$SUBJECT" }, SYST: { $addToSet: "$SYS" } } }, { "$unwind": "$SYST" }, { "$group": { _id: { "SUBJECT": "$_id", "SYST":"$SYST" } } }])
$unwindoperator in the first pipeline when theSYSfield is not an array? Why don't you just rundb.LogBuff.aggregate([ { "$group": { "_id": "$SUBJECT", "sys": { "$addToSet": "$SYS" } } } ])?