MongoDB $ group不支持包含样式表达式

时间:2016-03-30 22:45:15

标签: mongodb mongodb-query

我在“errmsg”:“$ group不支持包含式表达式”

db.lineitems.aggregate(
    { $match : { "shipdate" : { $lte: 19980801 }} },
    { $group : {
        _id : { "returnflag" :1, "linestatus" : 1},
        sum_qty : { $sum : "$quantity"},
        sum_base_price : { $sum : "$extendedprice"},
        sum_disc_price : { $sum : { $multiply : [ "$extendedprice",
        { $subtract : [1, "$discount"]}] }},
        sum_charge : { $sum : {$multiply : [ "$extendedprice",
        { $subtract : [1, "$discount"]}, {$add : [1, "$tax"]}] }},
        avg_qty : { $avg : "$quantity"},
        avg_price : { $avg : "$extendedprice"},
        avg_disc : { $avg : "$discount"},
        count_order : { $sum : 1}
        } },
    { $sort : {"_id.returnflag" : 1, "_id.linestatus" : 1}}
);

有一份文件样本:

{
"linenumber": 1,
"quantity": 41,
"extendedprice": 45682.2,
"discount": 0.02,
"tax": 0.08,
"returnflag": "N",
"linestatus": "O",
"shipdate": "Mon Jul 01 00:00:00 BST 1996",
"commitdate": "Sat Jul 20 00:00:00 BST 1996",
"receiptdate": "Fri Jul 12 00:00:00 BST 1996",
"shipinstruct": "TAKE BACK RETURN",
"shipmode": "SHIP",
"comment": "s. regular requ",
"order": {
    "orderkey": 535111,
    "orderstatus": "O",
    "totalprice": 48350.03,
    "orderdate": "Thu May 02 00:00:00 BST 1996",
    "orderpriority": "3-MEDIUM",
    "clerk": "Clerk#000000665",
    "shippriority": 0,
    "comment": "fluffily regular requests. f",
    "order": {
        "custkey": 10711,
        "name": "Customer#000010711",
        "address": "e3VJ63sxe8qAaKt 8 4daV0IE3CA9",
        "phone": "14-529-725-9738",
        "acctbal": 5983.81,
        "mktsegment": "BUILDING",
        "comment": "onic, stealthy ideas haggle carefully across the furi",
        "customer": {
            "nationkey": 4,
            "name": "EGYPT",
            "comment": "y above the carefully unusual theodolites. final dugouts are quickly across the furiously regular d",
            "region": {
                "regionkey": 4,
                "name": "MIDDLE EAST",
                "comment": "uickly special accounts cajole carefully blithely close requests. carefully final asymptotes haggle furiousl"
            }
        }
    }
},
"partsupp": {
    "availqty": 2155,
    "supplycost": 123.82,
    "comment": "arefully along the idly even accounts. asymptotes beside the slyly regular deposits boost since the busily unusual excus",
    "part": {
        "partkey": 10204,
        "name": "rosy chiffon blush burlywood white",
        "mfgr": "Manufacturer#5",
        "brand": "Brand#55",
        "type": "LARGE PLATED TIN",
        "size": 23,
        "container": "JUMBO PACK",
        "retailprice": 1114.2,
        "comment": " ironic ideas use care"
    },
    "supplier": {
        "suppkey": 985,
        "name": "Supplier#000000985",
        "address": "kzI8mk3jN9F67EStJ 8dlpx 6GwZYwzXPFOKJ5R",
        "phone": "11-131-656-2612",
        "acctbal": 3524.1,
        "comment": "ut the furiously final deposits integrate according to th",
        "nation": {
            "nationkey": 1,
            "name": "ARGENTINA",
            "comment": "al foxes promise slyly according to the regular accounts. bold requests alon",
            "region": {
                "regionkey": "1",
                "name": "AMERICA",
                "comment": "hs use ironic, even requests. s"
            }
        }
    }
}

} 我不知道是什么问题。我有150k这样的文件,我想得到这个结果,但我得到了这个错误。

2 个答案:

答案 0 :(得分:11)

你的问题在于

{ $group : {
    _id : { "returnflag" :1, "linestatus" : 1},  <---

此处&#34; returnfalg&#34;:1不受支持。它在$project中有效但不在$group如果您想创建复合键,请尝试

_id : { "returnflag" :"$returnflag", "linestatus" : "$linestatus"}

我相信您正试图按returnflaglinestatus

进行分组

至少应该修复你的错误。

答案 1 :(得分:0)

我已经得到了答案。我必须更改以下代码

_id : { "returnflag" :1, "linestatus" : 1},

_id : { "returnflag" :"$returnflag", "linestatus" : "$linestatus"},

蚂蚁有效。