我有一份文章清单,每篇文章都有一个数组属性,列出了其中提到的各种个人:
_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
函数,但未能使其正常工作。
group()
命令,早于聚合框架。请参阅:MongoDB聚合比较:group(),$group和MapReduce。 - Stennie