MongoDB - 在对另一列应用分组依据后获取列的不同值
MongoDB - Getting distinct values of a column after a group by on another column is applied
我有一个 Mongo table,其中包含年份、作者和 _id(比方说)。我需要做的就是过滤特定年份(比如 2006 年),对作者执行分组并获得每个作者的唯一 _id 列表。作者是一个数组。
我的table截图如下
{
"_id": ObjectId("54898fbb306ba2153c3e637b"),
"Author": [
"Hullman, J.",
"Drucker, S.",
"Riche, N.H.",
"Bongshin Lee",
"Fisher, D.",
"Adar, E."
],
"Year": "2013"
}{
"_id": ObjectId("54898fbb306ba2153c3e637c"),
"Author": [
"Schulz, H.",
"Nocke, T.",
"Heitzler, M.",
"Schumann, H."
],
"Year": "2013",
}{
"_id": ObjectId("54898fbc306ba2153c3e6b58"),
"Author": [
"Boissonnat, J.-D.",
"Geiger, B."
],
"Year": "1993",
}
目前我的查询如下
db.list_input_file.aggregate([
{'$match':{'Year':{$eq:'2006'}}},
{'$unwind':'$Author'},
{'$group':{'_id':'$Author', 'count': { '$sum': 1 }}}
])
我只需要输出格式为 [{"Author Name":[ids 列表]}, {"Author Name":[ids 列表]}]
我只需要使用 $addToSet 累加器运算符来获取 ID。我的最终查询如下
db.list_input_file.aggregate([
{'$match':{'Year':{$eq:'2006'}}},
{'$unwind':'$Author'},
{'$group':{'_id':'$Author', 'count': { '$sum': 1 }, 'ids':{$addToSet:'$_id'}}}
])
上面查询的输出快照如下所示。
{ "_id" : "Hofacker, I.L.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "Flamm, C.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "Heine, C.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "MacEachren, A.M.", "ids" : [ ObjectId("54898fbb306ba2153c3e66dd") ] }
{ "_id" : "Card, S.K.", "ids" : [ ObjectId("54898fbb306ba2153c3e66d4"), ObjectId("54898fbb306ba2153c3e66ca") ] }
我有一个 Mongo table,其中包含年份、作者和 _id(比方说)。我需要做的就是过滤特定年份(比如 2006 年),对作者执行分组并获得每个作者的唯一 _id 列表。作者是一个数组。
我的table截图如下
{
"_id": ObjectId("54898fbb306ba2153c3e637b"),
"Author": [
"Hullman, J.",
"Drucker, S.",
"Riche, N.H.",
"Bongshin Lee",
"Fisher, D.",
"Adar, E."
],
"Year": "2013"
}{
"_id": ObjectId("54898fbb306ba2153c3e637c"),
"Author": [
"Schulz, H.",
"Nocke, T.",
"Heitzler, M.",
"Schumann, H."
],
"Year": "2013",
}{
"_id": ObjectId("54898fbc306ba2153c3e6b58"),
"Author": [
"Boissonnat, J.-D.",
"Geiger, B."
],
"Year": "1993",
}
目前我的查询如下
db.list_input_file.aggregate([
{'$match':{'Year':{$eq:'2006'}}},
{'$unwind':'$Author'},
{'$group':{'_id':'$Author', 'count': { '$sum': 1 }}}
])
我只需要输出格式为 [{"Author Name":[ids 列表]}, {"Author Name":[ids 列表]}]
我只需要使用 $addToSet 累加器运算符来获取 ID。我的最终查询如下
db.list_input_file.aggregate([
{'$match':{'Year':{$eq:'2006'}}},
{'$unwind':'$Author'},
{'$group':{'_id':'$Author', 'count': { '$sum': 1 }, 'ids':{$addToSet:'$_id'}}}
])
上面查询的输出快照如下所示。
{ "_id" : "Hofacker, I.L.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "Flamm, C.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "Heine, C.", "ids" : [ ObjectId("54898fbb306ba2153c3e66e4") ] }
{ "_id" : "MacEachren, A.M.", "ids" : [ ObjectId("54898fbb306ba2153c3e66dd") ] }
{ "_id" : "Card, S.K.", "ids" : [ ObjectId("54898fbb306ba2153c3e66d4"), ObjectId("54898fbb306ba2153c3e66ca") ] }