[英]Mongo query to aggregate the size of arrays in entire collection in Robo3T
Here I attached my actual document structure in my mongo collection 'Order', I need to find the count of LineValueKey(element inside array) in my entire collection (I mean, I need to aggregate the size of LineValue array in the entire collection) .In my case, say I have these 2 documents in my collection so the count of LineValueKey should return 4. For this, I m trying to figure out mongo query. 在这里,我在mongo集合“ Order”中附加了实际的文档结构,我需要在整个集合中找到LineValueKey(数组内的元素)的数量(我的意思是,我需要在整个集合中汇总LineValue数组的大小)以我为例,说我的集合中有这两个文档,因此LineValueKey的计数应返回4。为此,我试图找出mongo查询。
I have already tried with below query in Robo 3T 1.2.1 but I am getting below error for my actual mongo Document structure however the same query working for different mongo structure. 我已经尝试过在Robo 3T 1.2.1中使用下面的查询,但是我的实际mongo文档结构却遇到了以下错误,但是同一查询适用于不同的mongo结构。 But I need to figure out query to count the LineValueKey for my actual mongo document structure. 但是我需要找出查询来为我的实际mongo文档结构计算LineValueKey。 Anyone can help on this please? 有人可以帮忙吗?
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
}
}
}
As pointed before, $size needs an array to work. 如前所述,$ size需要一个数组才能工作。 You have to transform your LineValue field from object to array. 您必须将LineValue字段从对象转换为数组。 To performs this, use $project stage with $objectToArray operator. 要执行此操作, 请将 $ project阶段与$ objectToArray运算符一起使用。
db.collection.aggregate([
{
$project: {
LineValue: {
$objectToArray: "$LineValue"
}
}
}
])
It will produce documents like this : 它将产生如下文件:
{
"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"
}
}
]
},
Then you can apply your $group stage, and count your array size. 然后,您可以应用$ group阶段,并计算数组大小。 Here's the complete aggregation : 这是完整的汇总:
db.collection.aggregate([
{
$project: {
LineValue: {
$objectToArray: "$LineValue"
}
}
},
{
$group: {
_id: null,
totalSize: {
$sum: {
$size: "$LineValue"
}
}
}
}
])
Will result with provided data: 将提供的数据作为结果:
[
{
"_id": null,
"totalSize": 4
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.