【发布时间】:2014-02-25 20:35:37
【问题描述】:
我有一个文章列表,每个文章都有一个数组属性,其中列出了其中提到的各种个人:
_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"
]
我一直在尝试(未成功)根据过去 7 天内标记的文章数量获取所有艺术家 (class_artist) 的列表。
我已经做到了:
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});
但不幸的是,它不是根据单个数组值计算它们,而是根据数组组合(即艺术家列表)。
我尝试使用$unwind 函数,但未能成功。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework