MongoDB聚合/映射减少
我是MongoDB的新手,我需要做一个聚合,这在我看来相当困难。文档看起来像这样MongoDB聚合/映射减少,mongodb,mapreduce,aggregation-framework,Mongodb,Mapreduce,Aggregation Framework,我是MongoDB的新手,我需要做一个聚合,这在我看来相当困难。文档看起来像这样 { "_id" : ObjectId("568192aef8bd6b0cd0f649c6"), "conference" : "IEEE International Conference on Acoustics, Speech and Signal Processing", "prism:aggregationType" : "Conference Proceeding", "children-i
{
"_id" : ObjectId("568192aef8bd6b0cd0f649c6"),
"conference" : "IEEE International Conference on Acoustics, Speech and Signal Processing",
"prism:aggregationType" : "Conference Proceeding",
"children-id" : [
"SCOPUS_ID:84948148564",
"SCOPUS_ID:84927603733",
"SCOPUS_ID:84943521758",
"SCOPUS_ID:84905234683",
"SCOPUS_ID:84876113709"
],
"dc:identifier" : "SCOPUS_ID:84867598678"
}
该示例仅包含聚合中所需的字段Prism:aggregationType可以有5个不同的值(会议进程、书籍、日志等)儿童id表示此文档被一系列其他文档引用(SCOPUS_id是每个文档的唯一id)。
我想做的是首先按会议进行分组,然后针对每个会议我想知道每个棱柱:聚合类型引用文档的数量($gt>0)
例如,假设有100个文档包含来自上面的会议。这100份文件被250份文件引用。我想知道从所有这250份文件中,有多少是“prism:aggregationType”:“会议进程”,“prism:aggregationType”:“Journal”等。
输出可以如下所示:
{
"conference" : "IEEE International Conference on Acoustics, Speech and Signal Processing",
"aggregationTypes" : [{"Conference Proceeding" : 50} , {"Journal" : 200}]
}
如果使用聚合管道或map reduce完成,这并不重要
编辑
有没有办法将这两个集合合并为一个集合:
db.articles.aggregate([
{ $match:{
conference : {$ne : null}
}},
{$unwind:'$children-id'},
{$group: {
_id: {conference: '$conference'},
'cited-by':{$push:{'dc:identifier':"$children-id"}}
}}
]);
db.articles.find( { 'dc:identifier': { $in: [ 'SCOPUS_ID:84943302953', 'SCOPUS_ID:84927603733'] } }, {'prism:aggregationType':1} );
在查询中,我想将$In中的数组替换为使用$push创建的数组,请尝试执行此操作 我们聊天的时候,
不幸的是,在聚合管道中使用$lookup会绑定到mongodb 3.2,这不是一种情况,因为R驱动程序可以使用mongo 2.6,并且源文档位于多个集合中 我在编辑部分编写的代码也是我得出的最终结果(稍加修改) 每次会议的结果如下所示:
{
"_id" : "Annual Conference on Privacy, Security and Trust",
"cited-by" : [
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84928151617",
"SCOPUS_ID:84939229259",
"SCOPUS_ID:84946407175",
"SCOPUS_ID:84933039513",
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84942607254",
"SCOPUS_ID:84948165954",
"SCOPUS_ID:84926379258",
"SCOPUS_ID:84946771354",
"SCOPUS_ID:84944223683",
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84939169499",
"SCOPUS_ID:84947104346",
"SCOPUS_ID:84948764343",
"SCOPUS_ID:84938075139",
"SCOPUS_ID:84946196118",
"SCOPUS_ID:84930820238",
"SCOPUS_ID:84947785321",
"SCOPUS_ID:84933496680",
"SCOPUS_ID:84942789431"
]
}
我遍历了我得到的所有文档(大约250个),然后在$in内使用引用的数组。我在dc:identifier上使用索引,所以它可以立即工作。
$lookup可以作为从聚合管道完成任务的替代方案,但R中的包不支持2.6以上的版本。
感谢您抽出时间:)鉴于上述示例是本系列中唯一的示例文档,您是否希望
“会议进程”
的计数与“儿童id”
数组的大小相同,即5?是的,如果此文档是唯一具有“会议”的文档:“IEEE声学、语音和信号处理国际会议”,则结果将等于阵列大小,但引用本文件的文件不一定具有相同的“prism:aggregationType”"; 但是可以肯定的是,会议会有更多的文档。这个想法是,你按会议分组,然后你将与数组中的ID进行内部连接,以获得这些ID的聚合类型,然后计算每个聚合类型Hi Anderw-这看起来是一个更大的问题-来聊天室我有3.0.6版的“R驱动程序”您所指的仅对“身份验证类型”有限制,实际上在版本中没有限制,还有其他驱动程序。当然,$lookup
仅在“服务器版本”3.2.x之后才可用,并且不会在OP提到的3.0.6版本中可用。Nonetheles,这是“同一个集合”,因此$lookup
不是您在这里使用的东西,这也是“附加结果”,而不是“相关结果”,正如$lookup
用于。另外,这是一个评论,并没有试图提供一个解决方案。是的-主要的事情是与3.0.6的联系
db.articles.aggregate([
{ $match:{
conference : {$ne : null}
}},
{$unwind:'$children-id'},
{$group: {
_id: '$conference',
'cited-by':{$push:"$children-id"}
}}
]);
db.articles.find( { 'dc:identifier': { $in: [ 'SCOPUS_ID:84943302953', 'SCOPUS_ID:84927603733'] } }, {'prism:aggregationType':1} );
{
"_id" : "Annual Conference on Privacy, Security and Trust",
"cited-by" : [
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84928151617",
"SCOPUS_ID:84939229259",
"SCOPUS_ID:84946407175",
"SCOPUS_ID:84933039513",
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84942607254",
"SCOPUS_ID:84948165954",
"SCOPUS_ID:84926379258",
"SCOPUS_ID:84946771354",
"SCOPUS_ID:84944223683",
"SCOPUS_ID:84942789431",
"SCOPUS_ID:84939169499",
"SCOPUS_ID:84947104346",
"SCOPUS_ID:84948764343",
"SCOPUS_ID:84938075139",
"SCOPUS_ID:84946196118",
"SCOPUS_ID:84930820238",
"SCOPUS_ID:84947785321",
"SCOPUS_ID:84933496680",
"SCOPUS_ID:84942789431"
]
}