Mongo 查询以聚合 Robo3T 中整个集合中数组的大小

Mongo query to aggregate the size of arrays in entire collection in Robo3T

这里我在我的 mongo 集合 'Order' 中附加了我的实际文档结构,我需要在我的整个集合中找到 LineValueKey(数组中的元素)的计数(我的意思是,我需要聚合整个集合中 LineValue 数组的大小)。在我的例子中,假设我的集合中有这两个文档,所以 LineValueKey 的计数应该 return 4。为此,我试图找出 mongo 查询.

我已经在 Robo 3T 1.2.1 中尝试使用以下查询,但我的实际 mongo 文档结构出现以下错误,但是相同的查询适用于不同的 mongo 结构。 但是我需要找出查询来计算我实际 mongo 文档结构的 LineValueKey。有人可以帮忙吗?

Query: db.getCollection('Order').aggregate([{$group: { _id: null, totalSize: { $sum: { $size: "$LineValue"}}} }])
Error : command failed: {
    "_t" : "OKMongoResponse",
    "ok" : 0,
    "code" : 17124,
    "errmsg" : "The argument to $size must be an array",
    "$err" : "The argument to $size must be an array"
} :
Query working Mongo Document structure:
{
    "_id" : {
        "OrderId" : 1,
        "OrderLineNumber" : 11
    },
    "OrderId" : 1,
    "OrderLineNumber" : 11,
    "vendorNbr" : 183020,
    "businessCode" : "Z",
    "itemNbr" : 552511439,
    "orderQty" : 0,    
    "LineValue" : [{
        "LineValueKey [uomCode=01]" : {
            "uomCode" : "01",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        }
        },{
        "LineValueKey [uomCode=02]" : {
            "uomCode" : "02",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        }
        }
    } ]
}
My case(2 documents) and Actual Document structure in Mongo Collection (Order)
{
    "_id" : {
        "OrderId" : 1,
        "OrderLineNumber" : 11
    },
    "OrderId" : 1,
    "OrderLineNumber" : 11,
    "vendorNbr" : 183020,
    "businessCode" : "Z",
    "itemNbr" : 552511439,
    "orderQty" : 0,    
    "LineValue" : {
        "LineValueKey [uomCode=01]" : {
            "uomCode" : "01",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        },
        "LineValueKey [uomCode=02]" : {
            "uomCode" : "02",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        }
    }
}

{
    "_id" : {
        "OrderId" : 2,
        "OrderLineNumber" : 12
    },
    "OrderId" : 2,
    "OrderLineNumber" : 12,
    "vendorNbr" : 183020,
    "businessCode" : "Z",
    "itemNbr" : 552511439,
    "orderQty" : 0,    
    "LineValue" : {
        "LineValueKey [uomCode=01]" : {
            "uomCode" : "01",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        },
        "LineValueKey [uomCode=02]" : {
            "uomCode" : "02",
            "costAmt" : 2.78,
            "packQty" : 1,
            "retailAmt" : 3.38
        }
    }
}

如前所述,$size 需要一个数组才能工作。您必须将 LineValue 字段从对象转换为数组。 要执行此操作,请使用 $project stage with $objectToArray 运算符。

db.collection.aggregate([
    {
        $project: {
          LineValue: {
            $objectToArray: "$LineValue"
          }
        }
      }
])

它将生成这样的文件:

{
    "LineValue": [
      {
        "k": "LineValueKey [uomCode=01]",
        "v": {
          "costAmt": 2.78,
          "packQty": 1,
          "retailAmt": 3.38,
          "uomCode": "01"
        }
      },
      {
        "k": "LineValueKey [uomCode=02]",
        "v": {
          "costAmt": 2.78,
          "packQty": 1,
          "retailAmt": 3.38,
          "uomCode": "02"
        }
      }
    ]
  },

然后你可以应用你的 $group 阶段,并计算你的数组大小。这是完整的聚合:

db.collection.aggregate([
  {
    $project: {
      LineValue: {
        $objectToArray: "$LineValue"
      }
    }
  },
   {
    $group: {
      _id: null,
      totalSize: {
        $sum: {
          $size: "$LineValue"
        }
      }
    }
  }
])

将使用提供的数据得出结果:

[
  {
    "_id": null,
    "totalSize": 4
  }
]