我有一个如下所示的Mongo聚合查询:
db.someCollection.aggregate([
{
$match: { taskId: "qy7u17-xunwqu" }
},
// Group by "tracklet_id" and calculate count for each group
{
$group: {
_id: '$tracklet_id',
count: { $sum: 1 },
representativeImage: { $first: '$img' }, // when I remove this, the query is done in a split second
timestamp: { $max: '$timestamp' },
},
},
{
$project: {
_id: 0,
trackletId: '$_id',
image: '$representativeImage', // but at the end, I want one representative image for a tracklet, doesn't matter which one.
timestamp: 1,
count: 1,
},
},
{
$sort: {
timestamp: -1
}
},
{
$limit: 20
},
], {allowDiskUse: true})
IMAGE字段包含占用大量内存的长B64字符串,导致查询的分组和排序阶段移到磁盘. 有没有办法在项目之前添加一个管道步骤,以便 for each tracklet重新包含一个图像字段?
我能想到的一种替代方法是之后只执行一个单独的查询来获得图像并合并结果,但我希望有一种更好的方法在相同的聚合查询中执行此操作.