MongoDB的组由数组内的元素 [英] MongoDB group by array inner-elements

查看:154
本文介绍了MongoDB的组由数组内的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文章列表,他们每个人都有列出在其中提到的各种个人数组属性:

I've got a list of articles, and each of them has an array property which lists various individuals mentioned in them:

_id: {
$oid: "52b632a9e4f2ba13c82ccd23"
},
providerName: "The Guardian",
url: "http://feeds.theguardian.com/c/34708/f/663860/s/3516cebc/sc/38/l/0L0Stheguardian0N0Cmusic0C20A130Cdec0C220Cwaterboys0Efishermans0Eblues0Etour0Ehammersmith/story01.htm",
subject: "The Waterboys – review",
class_artist: [
"paul mccartney"
]

我一直在试图(失败)来获取所有艺术家个人(class_artist)名单的基础上,他们已经在过去7天标记在文章的数量。

I've been trying (unsuccessfully) to get a list of all the individual artists (class_artist), based on the number of articles they've been tagged in within the past 7 days.

据我已经得到的:

var date = new Date();
date.setDate(date.getDate() - 7);
db.articles.group({
               key: { class_artist: 1 },
               cond: { class_date: { $gt: date } },
               reduce: function ( curr, result ) { result.cnt++; },
               initial: { cnt : 0 }
            }).sort({cnt: -1});

但遗憾的是,根据个人数组值并不指望他们,而是由数组组成(也就是艺术家的列表)。

But unfortunately, it doesn't count them based on the individual array values, but by array compositions (that is, lists of artists).

我尝试使用的 $开卷的功能,但一直没能使其发挥作用。

I tried using the $unwind function, but have not been able to make it work.

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

您使用的是什么框架?这并不是MongoDB的外壳,看起来像周围马preduce 一些奇怪的包装。在这种情况下 $放松将不可用,而你需要它的用户在< A HREF =htt​​p://docs.mongodb.org/manual/core/aggregation-pipeline/>聚合框架。这里是你想要在蒙戈外壳:

What framework are you using? This is not MongoDB shell and looks like some weird wrapper around MapReduce. In that case $unwind would not be available, and you need it for user in the aggregation framework. Here's what you want in the mongo shell:

db.articles.aggregate([
  {$match: { class_date: { $gte: date } } },
  {$project: { _id: 0, class_artist: 1 } },
  {$unwind: "$class_artist" },
  {$group: { _id: "$class_artist", tags: { $sum: 1 } }},
  {$project: { _id: 0,class_artist: "$_id", tags: 1 } },
  {$sort: { tags: -1 } }
])

如此高效:


  1. 过滤通过日期,因为你已经设置为最近7天无功

  2. 项目只有场(S),我们需要{我们只需要一个! }

  3. 开卷数组所以我们现在对每一个文档中的每个数组元素的纪录

  4. 的演出团体从扩展文件

  5. 项目到文档格式,可以按组使用带_id搞砸周围

  6. 排序结果以相反的顺序查看前标记的第一

  1. Filter by date because you already set a var for the last 7 days
  2. Project only the field(s) we need { We need only one! }
  3. Unwind the array so we now have a record for every array element in every document
  4. Group on the Artist from the expanded documents
  5. Project into a document format you can use as group messed around with _id
  6. Sort the results in reverse order to see the top tagged first

和有关聚集的伟大的事情是,你可以逐步建立这些阶段,看看到底是怎么回事。

And the great thing about aggregation is you can gradually build up those stages to see what is going on.

摇,烤到自己的驱动程序implmentation或ODM框架的要求。

Shake and bake into your own driver implmentation or ODM framework as required.

这篇关于MongoDB的组由数组内的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆