0

我在 mongo 中有以下文档:

{
        "_id" : ObjectId("501535acd729190bd62e3a58"),
        "o_orderkey" : NumberLong(1),
        "o_custkey" : {
                "$ref" : "customer",
                "$id" : ObjectId("5012e490cabc8baea9a541dd")
        },
        "o_orderstatus" : "O",
        "o_totalprice" : 173665.47,
        "o_orderdate" : ISODate("1996-01-02T02:00:00Z"),
        "o_orderpriority" : "5-LOW",
        "o_clerk" : "Clerk#000000951",
        "o_shippriority" : 0,
        "o_comment" : "blithely final dolphins solve-- blithely blithe packages nag blith",
        "o_lineitem" : [
                {
                        "_id" : ObjectId("501535abd729190bd62e38c7"),
                        "orderKey" : NumberLong(1),
                        "l_partkey" : {
                                "$ref" : "part",
                                "$id" : ObjectId("500f3a03d7292535356b839c")
                        },
                        "l_supplierkey" : {
                                "$ref" : "supplier",
                                "$id" : ObjectId("4ffed5dd125ee93ca6f3b294")
                        },
                        "l_linenumber" : 1,
                        "l_quantity" : 17,
                        "l_extendedprice" : 21168.23,
                        "l_discount" : 0.04,
                        "l_tax" : 0.02,
                        "l_returnflag" : "N"
                },
                {
                        "_id" : ObjectId("501535abd729190bd62e38c8"),
                        "orderKey" : NumberLong(1),
                        "l_partkey" : {
                                "$ref" : "part",
                                "$id" : ObjectId("500f398ed7292535356a2c54")
                        },
                        "l_supplierkey" : {
                                "$ref" : "supplier",
                                "$id" : ObjectId("4ffed5dd125ee93ca6f3b109")
                        },
                        "l_linenumber" : 2,
                        "l_quantity" : 36,
                        "l_extendedprice" : 45983.16,
                        "l_discount" : 0.09,
                        "l_tax" : 0.06,
                        "l_returnflag" : "N"
                }
        ]
}

当“o_shippriority”=0 和“l_linenumber”=1 时,我需要对“l_quantity”求和,我试过这个:

db.runCommand({ 
    mapreduce: "orders", 
    query: {
        o_shippriority: 0,
        "l_lineitem.l_linenumber": 1
    },
    map : function Map() {
        emit("sum",{this.o_lineitem}); 
    },
    reduce : function Reduce(key, values) {
        var sum = 0;
        for (var i = 0; i < values.length; i++) {
            var lineitem = values[i];
            for (var j=0; j<lineitem.length; j++) {
                sum += lineitem.l_quantity;
            }
        }
        return sum;
    },  
    out: 'query'
});

不起作用我收到“SyntaxError: missing : after property id (shell):8” 怎么了?

4

1 回答 1

0

您遇到的语法错误问题是由于以下行:

emit("sum",{this.o_lineitem});

哪个应该是正确的文档:emit("sum", {line:this.o_lineitem})或者根本不是文档:emit("sum", this.o_lineitem")取决于您要在 reduce 中执行的操作。

您必须首先解决的另一个问题query是 不会返回任何东西,您将需要"o_lineitem.l_linenumber": 1而不是"l_lineitem.l_linenumber": 1因为您的文档没有l_lineitem.

但是,您将在此查询中遇到更多问题,因为当您找到时,o_lineitem.l_linenumber:1您将返回整个文档 - 包括l_linenumber不是 1 的。您将需要遍历您正在发出的数组并仅在l_linenumber= 1。

于 2012-08-03T03:12:43.503 回答