【问题标题】:Mongo query to aggregate the size of arrays in entire collection in Robo3TMongo 查询聚合 Robo3T 中整个集合中数组的大小
【发布时间】:2020-01-13 08:31:51
【问题描述】:

在这里,我在我的 mongo 集合“Order”中附加了我的实际文档结构,我需要在我的整个集合中找到 LineValueKey(element inside array) 的计数(我的意思是,我需要在整个集合)。就我而言,假设我的集合中有这 2 个文档,因此 LineValueKey 的计数应返回 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 似乎是一个对象
  • 好的,在我的例子中 LineValue 实际上是一个包含 Map 的列表。如何将其转换为数组,需要在 java pojo 中添加任何注释以将其序列化为数组?

标签: mongodb mongodb-query robo3t


【解决方案1】:

如前所述,$size 需要一个数组才能工作。您必须将 LineValue 字段从对象转换为数组。 要执行此操作,请使用 $project 阶段和 $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
  }
]

【讨论】:

  • 非常感谢,这个查询真的有效!!!由于我是 Mongo 的新手,我将进一步挖掘您的查询以探索更多内容。
猜你喜欢
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多