总计和加在一起不起作用

时间:2018-12-11 09:27:45

标签: mongodb

我有一个脚本,该脚本可以在当前状态下工作,但是如果您取消注释组下的计数并尝试进行计数,则该脚本将不起作用,它会出错,不能完全确定原因。下面还提到了其他问题:

db.getCollection("9SP_Data").aggregate([

{"$match" : {"_id.object_category" : "revenue-transaction"}},
// include match for range of one year 
// include match for company
// maybe include extra parameters like origin_category or origin_name from customer settings for metric
    {"$unwind" :  "$line_items"},
    {"$match"  :  {"line_items.item_category":"sales-revenue"}},
    {"$group" : {
       "_id":
           {
            "company" : "$_id.company",
            "sum_by_date":  { $substrBytes: [ "$_id.transaction_date", 0, 6 ] },
            //  4 - by year
            //  6 - by month 
            //  8 - by date 
            // 10 - by hour 
            // 12 - by minute 
            "category" : "$line_items.item_category",
            "origin_category" : "$_id.object_origin_category",
            "origin_type" : "$_id.object_origin_type",
            "object_origin" : "$_id.object_origin"},
            "metric_value"  : { $sum: "$line_items.item_net_total_value" },
//                { $count: "metric_volume" }
}},
{"$project" : {
    "_id.company"               : "$_id.company",
    "_id.metric_name"           : {$literal : "revenue"},
    "_id.metric_category"       : {$literal : "sales"},
    "_id.metric_type"           : {$literal : "month"},
    "_id.metric_lookup"         : "$sum_by_date",
    "_id.object_origin_category": "$_id.origin_category",
    "_id.object_origin_type"    : "$_id.object_origin_type",
    "_id.object_origin"         : "$_id.object_origin",
    "metric_value"              : "$metric_value"
//        ,"metric_volume"             : "$metric_volume"
}}
])

我以前没有使用过$ count,并且mongoDb文档没有帮助我。

我以当前形式得到的结果是:

{
"_id" : {
    "metric_name" : "revenue",
    "metric_category" : "sales",
    "metric_type" : "month",
    "object_origin_category" : "point-of-sale",
    "object_origin" : "vend"
},
"metric_value" : 2099.9997
}

我没有陪伴,但我知道为什么,它在我的数据集中不存在。

我不确定为何_id.object_origin_type没有通过,而类别却是

不知道为什么_id.metric_lookup也不会出现

不是来自有问题的$ count的metric_volume

以下是聚合使用的原始文档示例:

{
"_id" : {
    "connection" : "cb1c4a56-1544-4e9d-a433-abb33429a300",
    "transaction_date" : 20170714020700,
    "transaction_date_utc" : "2017-07-14 02:07:00",
    "object_class" : "goods-service-transaction",
    "object_category" : "revenue-transaction",
    "object_type" : "receipt",
    "object_origin_category" : "point-of-sale",
    "object_origin_type" : "offline",
    "object_origin" : "vend",
    "transaction_status" : "CLOSED",
    "related_reference" : "41"
},
"object_creation_date" : "20181210120902",
"party_identifier" : "WALKIN",
"staff_identifier" : "02dcd191-ae2b-11e6-f485-7967ed9c6343",
"staff_name" : "uat1@9spokes.com",
"line_items" : [
    {
    "item_name" : "Dress Shirt / Polyester / Large",
    "item_system_id" : "02dcd191-ae20-11e6-f485-7967eef9a797",
    "item_identifier" : "10024",
    "item_category" : "sales-revenue",
    "item_type" : "goods-service",
    "item_quantity" : 1,
    "item_net_unit_sale_value" : 61.3636,
    "item_net_unit_discount_value" : 0,
    "item_unit_tax_value" : 6.8182,
    "item_net_total_value" : 61.3636,
    "item_total_tax_value" : 6.81818,
    "item_total_gross_value" : 68.18182
}
]
}

感谢您的帮助。马特

1 个答案:

答案 0 :(得分:0)

$ count是Aggregation Pipeline Stage,您尝试将其用作聚合管道运算符。 您必须将$ count放在$ group之后,然后再计算组数(如果需要的话),或者简单地使用诸如“ metric_volume”:{$ sum:1}(当然是_id声明之后)之类的东西来对分组文档进行计数。 / p>