Mongo汇总:使用$ count找到的文档进行查询

时间:2020-07-08 10:21:16

标签: mongodb dictionary count let

我有一个Mongo集合系列,其中每个文档都有一个带有 dataPoints 的列表。 具有相同 testStepId 的所有系列包含相同数量的 dataPoints

{
  "seriesId": {
    "seriesId": "77678ca1-31db-4cec-a042-68a3053b92c6"
  },
  "testStepId": {
    "testStepId": "c152415b-2392-4c2b-af74-51a4973bd257"
  },
  "measurement": {
    "startTime": {
      "$date": "2020-07-07T12:40:49.782Z"
    },
    "endTime": {
      "$date": "2020-07-07T12:42:19.782Z"
    }
  },
  "dataPoints": [
    {
      "timeStamp": {
        "$date": "2020-07-07T12:41:09.782Z"
      },
      "value": "Value_1_1"
    },
    {
      "timeStamp": {
        "$date": "2020-07-07T12:41:29.782Z"
      },
      "value": "Value_1_2"
    },
    {
      "timeStamp": {
        "$date": "2020-07-07T12:41:39.782Z"
      },
      "value": "Value_1_3"
    },
    ...
    {
      "timeStamp": {
        "$date": "2020-07-07T12:42:19.782Z"
      },
      "value": "Value_2_11"
    }
  ]
}

现在,我要查询与特定 testStepId 匹配的所有系列文档(没问题)。 但是,我不想加载所有找到的所有系列中的所有 dataPoints ,而是只加载1000个 dataPoints 。 因此,在找到10个系列的情况下,每个系列只需要加载100个 dataPoints

->加载每(dataPoints.size()/ 100)个dataPoint

->这意味着我必须考虑找到的系列文档的数量和该系列中 dataPoints 的数量

->加载每个第X个 dataPoint 其中

X = 1000 / <count of documents> / <count of dataPoints>

我正在努力通过与MongoDB Compass的聚合来完成此任务。但是我仍然无法计算找到的文档并取消设置此值...

为了简单起见,我只尝试获取每个第二个 dataPoint

{
    project: {
        dataPoints: {
            $map: {
                input: { $range: [ 0, {"$size": "$dataPoints"}, 2 ] },
                as: "index",
                in: { $arrayElemAt: [ "$dataPoints", "$$index" ] }
            }
        }
    }
}

->工作正常

现在,我想根据找到的文档数来获取每个第x个“ dataPoint”依赖项。 为此,我尝试了一些不同的方法,但都没有效果...

  1. 尝试:使用 $ count 代替固定数字:
{
    project: {
        dataPoints: {
            $map: {
                input: { $range: [ 0, {"$size": "$dataPoints"}, $count ] },
                as: "index",
                in: { $arrayElemAt: [ "$dataPoints", "$$index" ] }
            }
        }
    }
}

-> “项目规范必须是一个对象”

  1. try:将 count 定义为变量:
{
    project: {
        dataPoints: {
            $let: {
                vars: { 
                    total: "$count",
                },
                in: { 
                    $map: {
                        input: { $range: [ 0, {"$size": "$dataPoints"}, "$$total"] },
                        as: "index",
                        in: { $arrayElemAt: [ "$dataPoints", "$$index" ] }
                    }
                }
            }
        }
    }   
}

-> “ $ range需要一个数字值,找到的类型为:”

显然我的方法是错误的。 有人能给我一些提示如何使它工作吗?

2 个答案:

答案 0 :(得分:1)

我认为X的公式是X = <count of dataPoints> * <count of documents> / 1000

您不能在特定的聚合管道阶段直接访问文档数(数量)。但是,您可以将所有文档合并为一个文档并进行计数,然后将它们扩展回单独的文档中。您可以使用$group$facet来实现。

我将举例说明$group

[
  {
    $group: {
      _id: null,
      count: { $sum: 1 },
      all: { $push: "$$ROOT" }
    }
  },
  {
    $unwind: "$all"
  },
  {
    $replaceWith: { // $replaceWith is available from v4.2, for earlier version use { $replaceRoot: { newRoot: <doc> } }
      $mergeObjects: [
        "$all",
        {
          dataPoints: {
            $map: {
              input: {
                $range: [
                  0,
                  { $size: "$all.dataPoints" },
                  {
                    $ceil: {
                      $divide: [
                        {
                          $multiply: [
                            { "$size": "$all.dataPoints" },
                            "$count"
                          ]
                        },
                        1000
                      ]
                    }
                  }
                ]
              },
              as: "index",
              in: { $arrayElemAt: ["$all.dataPoints", "$$index"] }
            }
          }
        }
      ]
    }
  }
]

Mongo Playground

答案 1 :(得分:0)

在mongo专家的支持下找到了一个非常好的解决方案:

[{
    //
    // Group the series
    //
    '$group': {
        '_id': {
            'seriesName': '$series.seriesName'
        }, 
        'dataPoints': {
            '$push': '$dataPoints'
        }, 
        'series': {
            '$addToSet': '$series'
        }
    }
}, 
{
    //
    // Concat the dataPoints for each series into on array
    //
    '$addFields': {
        'dataPoints': {
            '$reduce': {
                'input': '$dataPoints', 
                'initialValue': [], 
                'in': {
                    '$concatArrays': [
                        '$$value', '$$this'
                    ]
                }
            }
        }
    }
}, 
{
    //
    // Calculate 'x' for 'find every x-th dataPoint' (called index here)
    // 
    '$replaceWith': {
        'dataPoints': {
            '$map': {
                'input': {
                    '$range': [
                        0, {
                            '$size': '$dataPoints'
                        }, {
                            '$ceil': {
                                '$divide': [
                                    {
                                        '$size': '$dataPoints'
                                    }, 100
                                ]
                            }
                        }
                    ]
                }, 
                'as': 'index', 
                'in': {
                    '$arrayElemAt': [
                        '$dataPoints', '$$index'
                    ]
                }
            }
        }
    }
}]

提示:这将不会返回确切的dataPoints数量,而是返回一个近似值。但这正是我所需要的...

MongoPlayground