我有一个如下所示的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重新包含一个图像字段?

我能想到的一种替代方法是之后只执行一个单独的查询来获得图像并合并结果,但我希望有一种更好的方法在相同的聚合查询中执行此操作.

推荐答案

As verified by the OP.
Instead of having the image in the $group stage get the image from a self-lookup where you limit only to 1 doc (since we only need any single image and to save memory) that matches the same trackletid

db.someCollection.aggregate([
  { $match: { taskId: "qy7u17-xunwqu" } },
  {
    $group: {
      _id: "$tracklet_id",
      count: { $sum: 1 },
      timestamp: { $max: "$timestamp" }
    }
  },
  { $project: { _id: 0, trackletId: "$_id", timestamp: 1, count: 1 } },
  { $sort: { timestamp: -1 } },
  { $limit: 20 },
  {
    $lookup: {
      from: "someCollection",
      let: { trackletId: "$trackletId" },
      pipeline: [
        { $match: { $expr: { $eq: [ "$tracklet_id", "$$trackletId" ] } } },
        { $limit: 1 },
        { $project: { _id: 0, img: 1 } }
      ],
      as: "image"
    }
  },
  { $addFields: { image: { $arrayElemAt: [ "$image.img", 0 ] } } }
])

demo

Mongodb相关问答推荐

在MongoDB中使用explain()和查询时缺少winningPlan''''

MongoDB聚合如何对对象数组中的值求和

为什么这个查询可以在MongoDB中使用?

匹配/筛选/投影对象中数组中数组中的嵌套字段

MongoDB 投影按数组中的字符串长度排序

如何在 MongoDB 中的集合下查找同一文档

来自嵌套对象数组的 MongoDB 聚合

通过insertId MongoDB获取文档

在运算符 $map 中嵌入运算符 $exists

mongo:在 mongodb 6.0 docker 容器上找不到命令

如何在 Mongoose 中定义一个通用的嵌套对象

无法提取地理键,经度/纬度超出范围

MongoDB 使用自定义表达式或函数进行排序

Mongodb:查询嵌套在数组中的json对象

spring-data-mongo - 可选查询参数?

MongoDB - Permission denied for socket: /tmp/mongodb-27017.sock

Mongodb错误:The positional operator did not find the match needed from the query

我如何将 mongodb 与electron一起使用?

我可以只获取 Cursor 对象(pymongo)中的第一项吗?

我需要手动关闭mongoose连接吗?