如何访问MapReduce MongoDB中的emebed对象



我在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:丢失:在属性id(shell)之后:8"怎么了?

语法错误的问题是因为以下行:

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时,您将返回整个文档,包括不是1的l_linenumber。当l_linenumber=1时,您将需要遍历正在发射的数组并求和。

相关内容

  • 没有找到相关文章

最新更新