我有一个包含Year,Author和_id的Mongo表(比方说)。我需要做的就是过滤特定的一年(比如2006),在作者上执行分组,并为每个作者获取一个唯一的_id列表。作者是一个数组。
我的表格快照如下
{
"_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 }}}
])
我需要的只是格式[{“作者姓名”:[ids列表]},{“作者姓名”:[ids列表]}]
的输出答案 0 :(得分:2)
我只需使用$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") ] }