2

我有一个“文章”集合,一些示例数据可能如下所示:

[
{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 查询和不同的聚合,但没有结果。

4

3 回答 3

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
于 2012-10-05T13:00:41.133 回答
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' } ]
于 2012-10-05T12:34:31.583 回答
3
db.articles.distinct("tags")

给出以下输出:

[
{
    "tid" : 24,
    "name" : "Siberia"
},
{
    "tid" : 5231,
    "name" : "Lolcats"
},
{
    "tid" : 6432,
    "name" : "France"
}
]
于 2012-10-05T13:07:57.403 回答