MongoDB聚合,如何在组管道中addToSet数组的每个元素

编程入门 行业动态 更新时间:2024-10-25 00:25:49
本文介绍了MongoDB聚合,如何在组管道中addToSet数组的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有包含标签字段的文档.这是一个简单的数组,里面有标签名,里面没有对象,也没有_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"] } } }, } } );

推荐答案

模拟带有$ each修饰符的$ addToSet更新运算符在聚合管道中,您可以结合使用 $ reduce + $ 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数组的每个元素

本文发布于:2023-11-22 19:38:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   道中   元素   如何在   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!