我无法限制具有聚合管道的组函数中推送的元素的数量。这个是可能的吗?小示例:
数据:
[
{
"submitted": date,
"loc": {
"lng": 13.739251,
"lat": 51.049893
},
"name": "first",
"preview": "my first"
},
{
"submitted": date,
"loc": {
"lng": 13.639241,
"lat": 51.149883
},
"name": "second",
"preview": "my second"
},
{
"submitted": date,
"loc": {
"lng": 13.715422,
"lat": 51.056384
},
"name": "nearpoint2",
"preview": "my nearpoint2"
}
]
下面是我的聚合管道:
var pipeline = [{
//I want to limit the data to a certain area
$match: {
loc: {
$geoWithin: {
$box: [
[locBottomLeft.lng, locBottomLeft.lat],
[locUpperRight.lng, locUpperRight.lat]
]
}
}
}
},
// I just want to get the latest entries
{
$sort: {
submitted: -1
}
},
// I group by name
{
$group: {
_id: "$name",
< --get name
submitted: {
$max: "$submitted"
},
< --get the latest date
locs: {
$push: "$loc"
},
< --push every loc into an array THIS SHOULD BE LIMITED TO AN AMOUNT 5 or 10
preview: {
$first: "$preview"
}
}
},
//Limit the query to at least 10 entries.
{
$limit: 10
}
];
如何将locs
数组限制为10
或任何其他大小?我用$each
和$slice
尝试了一些东西,但似乎不起作用。
https://stackoverflow.com/questions/24594049
复制相似问题