人。如果我有这样的mongo集合:
{ "author" : "author1", "title" : "title1", "edition" : "2010" }
{ "author" : "author2", "title" : "title1", "edition" : "2011" }
{ "author" : "author1", "title" : "title3", "edition" : "2010" }
我怎么能得到这样的东西:
{"author":["author1", "author2"], "title":["title1", "title3"], "edition":["2010", "2011"]}
在使用distinct()?
的单个查询中我不需要这些项目的任何组合,我需要唯一值的数组
答案 0 :(得分:0)
您可以在$group
聚合
db.col.aggregate([
{
$group: {
_id: null,
author: { $addToSet: "$author" },
title: { $addToSet: "$title" },
edition: { $addToSet: "$edition" }
}
}
])