MongoDB聚合,如何在组管道中addToSet数组的每个元素 [英] MongoDB aggregate, how to addToSet each element of array in group pipeline
问题描述
我有包含标签字段的文档.这是一个简单的数组,里面有标签名,里面没有对象,也没有_id.
像这样的普通标签["Protocol", "Access", "Leverage", "Capability"]
.
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"]
.
在我的小组管道中,我尝试了类似'selectedTags': { $addToSet: '$tags' }
的方法,但是最终我得到了一个包含标签数组的数组.而且$push
也是如此.
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
.
我尝试使用$each
或$pushAll
,但是正如我的Shell告诉我的那样,它们不被用作分组运算符.
I tried to use $each
or $pushAll
but they are not supported as grouping operator as my shell tell me.
有人可以帮我吗?
谢谢
示例文档:
{
"_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")
}
汇总:
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 } }
);
然后我尝试将selectedTags更改为{ $push: { $each: '$tags' } }
或{ $pushAll: '$tags' }
,但这不执行:(
then I tried to change the selectedTags to { $push: { $each: '$tags' } }
or { $pushAll: '$tags' }
but this does not execute :(
在javascript中,我是这样做的:
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"] }
}
},
} }
);
推荐答案
模拟 $ setUnion 在投影阶段.例如:
To mimic functionality of $addToSet update operator with $each modifier in aggregation pipeline you can use a combination of $push on grouping stage and $reduce + $setUnion on projection stage. E.g.:
db.collection.aggregate([
{$group:{
_id: null,
selectedTags: { $push: '$tags' }
}},
{$project: {
selectedTags: { $reduce: {
input: "$selectedTags",
initialValue: [],
in: {$setUnion : ["$$value", "$$this"]}
}}
}}
])
将得到一个文档,其中包含与selectedTags
数组中的所有文档不同的标签列表.
results with a single document which contains a distinct list of tags from all documents in selectedTags
array.
这篇关于MongoDB聚合,如何在组管道中addToSet数组的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!