我有一个'文章'集合,一些示例数据可能如下所示:
[
{body: 'Interesting news in Siberia and so on etc. etc. etc. and lolcats too',
author: 'John Doe',
tags: [{tid:24, name: "Siberia"},
{tid: 5231, name: "Lolcats"},]
},
{body: 'Something is going on in Siberia and France',
author: 'Jane Doe',
tags: [{tid:24, name: "Siberia"},
{tid: 6432, name: "France"},]
},
]
我所需的输出是一个独特的标签列表:
[
{tid: 24, name: 'Siberia'},
{tid: 5231, name: 'Lolcats'},
{tid: 6432, name: 'France'},
]
我一直在努力处理一些mapReduce查询和不同的聚合,但没有结果。
答案 0 :(得分:4)
最简单的方法是:
db.articles.distinct("tags")
如果你想使用聚合框架(2.2中的新内容),那就更长了一点:
db.articles.aggregate([{$unwind:"$tags"},
{$group:{_id:"$tags"}},
{$project:{tid:"$_id.tid",name:"$_id.name",_id:0}}
]).result
答案 1 :(得分:3)
在mongo v2.2中,您可以使用aggregate
功能执行此操作:
db.articles.aggregate([
{
// From each document, emit just the tags
$project: {
tags: 1
}
}, {
// Duplicate each document for each tags element it contains
$unwind: '$tags'
}, {
// Group the documents by the tag's tid and name
$group: {
_id: { tid: '$tags.tid', name: '$tags.name' }
}
}, {
// Reshape the document to exclude the _id and bring tid and name to the top level
$project: {
_id: 0,
tid: '$_id.tid',
name: '$_id.name'
}
}],
function (err, result) {
if (err) {
console.log('aggregation error: %s', err);
} else {
console.dir(result);
}
});
对于您的文档,这会产生以下输出:
[ { tid: 6432, name: 'France' },
{ tid: 5231, name: 'Lolcats' },
{ tid: 24, name: 'Siberia' } ]
答案 2 :(得分:3)
db.articles.distinct("tags")
给出以下输出:
[
{
"tid" : 24,
"name" : "Siberia"
},
{
"tid" : 5231,
"name" : "Lolcats"
},
{
"tid" : 6432,
"name" : "France"
}
]