聚合查询中的MongoDB限制数组 [英] Mongodb limit array within aggregate query
问题描述
我正在尝试编写查询以返回每个类别中的前X个字词-例如前5个,前10个等.每个术语都有一个相关的类别,并基于另一个 stackoverflow问题的一些帮助. a>我设法做到了:
I'm attempting to write a query to return the top X terms across each category - e.g. top 5, top 10 etc. Each term has an associated category, and based up on some help from another stackoverflow question I've managed to get this:
db.collection.aggregate([
{
$group : {
_id : {
category: "$uri.category",
term: "$uri.term",
},
total: { $sum : 1 }
}
},
{ $sort : { total : -1 } },
{
$group : {
_id : "$_id.category",
terms: {
$push: {
term: "$_id.term",
total: "$total"
}
}
}
}
]);
上面的查询确实起作用,并且返回看起来像这样的数据:
The above query does work, and returns data that looks something like this:
[
{ category: "movies",
terms: [ { term: "movie 1", total: 5000 }, { term: "movie 2", total: 200 } ... ]
},
{ category: "sports",
terms: [ { term: "football 1", total: 4000 }, { term: "tennis 2", total: 250 } ... ]
},
]
但是我试图将条件数组限制为一个固定的数字,即5或10-这将对应于每个类别的X次搜索.我一直在尝试各种选择,例如在$push
内添加$slice
来减少条件数组,但没有成功.
However I'm trying to limit the terms array to a fixed number i.e. 5 or 10 - this will correspond to the X number of searches per category. I've been trying various options such as adding $slice
within the $push
to reduce the terms array down with no success.
这可以使用聚合框架来实现,还是我应该考虑另一种方法?
Can this be achieved using the aggregate framework, or should I look at another approach?
推荐答案
从Mongodb 2.6开始,使用$slice
或$push
和.aggregate()
函数/命令限制数组大小的能力是不支持.
这是MongoDb问题跟踪器上的功能请求.
It seems as of Mongodb 2.6, the ability to limit the size of an array using $slice
or $push
with the .aggregate()
function/command is unsupported.
Here's the feature request on the MongoDb issue tracker.
我要做的是将汇总结果输出到集合中.然后更新收藏集.
What I would do is output the aggregated result to an collection. Then update the collection.
use test;
var rInt = function(x) {
return 1 + ~~(Math.random() * x);
};
var rObj = function() {
return {
"timestamp": new Date(),
"category": "movies" + rInt(5),
"term": "my movie" + rInt(20)
}
};
for (var i = 0, l = 100; i < l; i++) {
db.al.insert(rObj());
}
汇总查询
db.al_out.drop();
db.al.aggregate([
{
$group : {
_id : {
category: "$category",
term: "$term",
},
total: { $sum : 1 }
}
},
{ $sort : { total : -1 } },
{
$group : {
_id : "$_id.category",
terms: {
$push: {
term: "$_id.term",
total: "$total"
}
}
}
}
,{ $out : "al_out" } // output the documents to `db.al_out`
]);
// limit the size of terms to 3 elements.
db.al_out.update( {}, {
$push : {
terms : {
$each : [],
$slice : 3
}
}
}, {
multi:true
});
结果:
db.al_out.find();
{ "_id" : "movies1", "terms" : [ { "term" : "my movie7", "total" : 3 }, { "term" : "my movie6", "total" : 3 }, { "term" : "my movie17", "total" : 2 } ] }
{ "_id" : "movies2", "terms" : [ { "term" : "my movie3", "total" : 4 }, { "term" : "my movie11", "total" : 2 }, { "term" : "my movie2", "total" : 2 } ] }
{ "_id" : "movies4", "terms" : [ { "term" : "my movie9", "total" : 3 }, { "term" : "my movie1", "total" : 3 }, { "term" : "my movie7", "total" : 2 } ] }
{ "_id" : "movies3", "terms" : [ { "term" : "my movie19", "total" : 5 }, { "term" : "my movie8", "total" : 4 }, { "term" : "my movie14", "total" : 4 } ] }
{ "_id" : "movies5", "terms" : [ { "term" : "my movie7", "total" : 6 }, { "term" : "my movie17", "total" : 4 }, { "term" : "my movie3", "total" : 2 } ] }
这篇关于聚合查询中的MongoDB限制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!