我有一个文章列表,每个文章都有一个数组属性,列出了其中提到的各个人:
_id: {
$oid: "52b632a9e4f2ba13c82ccd23"
},
providerName: "The Guardian",
url: "http://feeds.theguardian.com/c/34708/f/663860/s/3516cebc/sc/38/l/0L0Stheguardian0N0Cmusic0C20A130Cdec0C220Cwaterboys0Efishermans0Eblues0Etour0Ehammersmith/story01.htm",
subject: "The Waterboys – review",
class_artist: [
"paul mccartney"
]
Run Code Online (Sandbox Code Playgroud)
我一直在努力(不成功)class_artist根据他们在过去7天内被标记的文章数量来获取所有个体艺术家的列表().
我已经达到了:
var date = new Date();
date.setDate(date.getDate() - 7);
db.articles.group({
key: { class_artist: 1 },
cond: { class_date: { $gt: date } },
reduce: function ( curr, result ) { result.cnt++; },
initial: { cnt : 0 }
}).sort({cnt: -1});
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它并不是基于单个数组值来计算它们,而是基于数组合成(即艺术家列表).
我尝试使用该$unwind功能,但无法使其工作.
我可能会对此有点了解,因为我还在学习MongoDB的细节,但是这里有.
现在我正在研究一种搜索/过滤数据集的工具,按任意数据点(例如流行度)对其进行排序,然后按ID进行分组.我认为我能做到这一点的唯一方法是通过Mongo的MapReduce功能.
我不能使用.group(),因为我正在使用超过10,000个键,我还需要能够对数据集进行排序.
我的MapReduce代码运行得很好,除了一件事:排序.排序只是不想工作.
db.runCommand({
'mapreduce': 'products',
'map': function() {
emit({
product_id: this.product_id,
popularity: this.popularity
}, 1);
},
'reduce': function(key, values) {
var sum = 0;
values.forEach(function(v) {
sum += v;
});
return sum;
},
'query': {category_id: 20},
'out': {inline: 1},
'sort': {popularity: -1}
});
我已经在流行度数据点上有一个降序索引,所以它肯定不起作用,因为缺少:
{ "v" : 1, "key" : { "popularity" : -1 }, "ns" : "app.products", "name" : "popularity_-1" }
我只是想不通为什么它不想排序.
我没有内联结果集,而是将其输出到另一个集合,然后运行.find().sort({popular:-1}),因为这个功能的工作方式.
实际上,我使用map reduce来进行一些计算.我不能用聚合框架做到这一点,因为我的计算没有可用的管道运算符.
是否可以编写自定义管道运算符?
提前致谢
我想知道我们如何进行以下从 sql 到 mongoDB 的转换:
假设该表具有以下结构:
table
=====
-----
##id contribution time
1 300 Jan 2, 1990
2 1000 March 3, 1991
Run Code Online (Sandbox Code Playgroud)
我想找到一个按照贡献数量降序排列的 ids 排名列表。
'$' 这是我使用 sql 所做的:
select id, count(*) c from table group by id order by c desc;
Run Code Online (Sandbox Code Playgroud)
如何使用 count()、order() 和 group() 将这个复杂的 sql 转换为 mongoDB?
非常感谢!