I have documents that contains a tags fields. It's a simple array with tag names inside, no object nor _id inside.
Just plain tags like this ["Protocol", "Access", "Leverage", "Capability"]
.
And in my group pipeline I tried something like 'selectedTags': { $addToSet: '$tags' }
but then I end up with an array containing arrays of tags. And I get the same with $push
.
I tried to use $each
or $pushAll
but they are not supported as grouping operator as my shell tell me.
Can someone help me on this one please ?
Thank you
Edit:
Sample docs:
{
"_id" : "HWEdDGsq86x4ikDSQ",
"teamId" : "AdLizGnPuqbWNsFHe",
"ownerId" : "Qb5EigWjqn2t3bfxD",
"type" : "meeting",
"topic" : "Grass-roots hybrid knowledge user",
"fullname" : "Guidouil",
"startDate" : ISODate("2017-07-30T09:00:05.513Z"),
"shareResults" : true,
"open" : true,
"language" : "fr",
"tags" : [
"Protocol",
"Challenge",
"Artificial Intelligence",
"Capability"
],
"isDemo" : true,
"createdAt" : ISODate("2017-11-15T19:24:05.513Z"),
"participantsCount" : 10,
"ratersCount" : 10,
"averageRating" : 3.4,
"hasAnswers" : true,
"updatedAt" : ISODate("2017-11-15T19:24:05.562Z")
}
{
"_id" : "rXvkFndpXwJ6KAvNo",
"teamId" : "AdLizGnPuqbWNsFHe",
"ownerId" : "Qb5EigWjqn2t3bfxD",
"type" : "meeting",
"topic" : "Profit-focused modular system engine",
"fullname" : "Guidouil",
"startDate" : ISODate("2017-07-24T12:00:05.564Z"),
"shareResults" : true,
"open" : true,
"language" : "fr",
"tags" : [
"Initiative",
"Artificial Intelligence",
"Protocol",
"Utilisation"
],
"isDemo" : true,
"createdAt" : ISODate("2017-11-15T19:24:05.564Z"),
"participantsCount" : 33,
"ratersCount" : 33,
"averageRating" : 2.9393939393939394,
"hasAnswers" : true,
"updatedAt" : ISODate("2017-11-15T19:24:05.753Z")
}
Aggregation:
db.surveys.aggregate(
{ $match: query },
{
$group: {
'_id': {
'year': { $year: '$startDate' },
'day': { $dayOfYear: '$startDate' },
},
'participants': { $sum: '$ratersCount' },
'rating': { $avg: '$averageRating' },
'surveys': { $push: '$_id' },
'selectedTags': { $addToSet: '$tags' },
'peoples': { $addToSet: '$fullname' },
}
},
{ $sort: { _id: 1 } }
);
then I tried to change the selectedTags to { $push: { $each: '$tags' } }
or { $pushAll: '$tags' }
but this does not execute :(
Edit 2:
In javascript I do it like that:
return Surveys.aggregate(
{ $match: query },
{ $group: {
_id: dateGroup,
participants: { $sum: '$ratersCount' },
rating: { $avg: '$averageRating' },
surveys: { $push: '$_id' },
selectedTags: { $push: '$tags' },
peoples: { $addToSet: '$fullname' },
} },
{ $project: {
_id: null,
selectedTags: {
$reduce: {
input: "$selectedTags",
initialValue: [],
in: { $setUnion: ["$$value", "$$this"] }
}
},
} }
);