2012-08-02 3 views
0

나는 몽고에서 다음 문서 : 1 나는이 시도했다 =MapReduce MongoDB에서 개체를 어떻게 액세스 했나요?

{ 
     "_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" 
       } 
     ] 
} 

을 그리고 난 "l_quantity" "o_shippriority"= 0 "l_linenumber"을 요약해야

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 : 속성 ID (셸) : 8 이후" 뭐가 잘못 되었나요?

+0

인 경우에만 합계가 표시되고 스키마에 따라 o_lineitem.l_linenumber 여야합니다. 방출이 정확하지 않습니다. 숫자를 출력해야하므로 주위에 {}이 없어야합니다. 하지만 정확히 당신이하려는 것은 무엇입니까? –

답변

0

당신이 구문 오류가 발생하는 문제 때문에 라인이다 :

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

어느해야 하나 적절한 문서 수 : emit("sum", {line:this.o_lineitem}) 또는 모든 문서하지 : emit("sum", this.o_lineitem") 당신이려고하고있는 무슨에 따라 당신이 줄이자. 당신이로 시작해야합니다

다른 문제는 문서가 l_lineitem을 가지고 있지 않기 때문에 당신은 "o_lineitem.l_linenumber": 1 대신 "l_lineitem.l_linenumber": 1이 필요합니다의 query이 아무것도 반환하지 않을 것입니다.

o_lineitem.l_linenumber:1을 찾을 때 l_linenumber이 아닌 1을 포함하여 전체 문서를 반환하기 때문에이 쿼리에 더 많은 문제가있을 것입니다. 배열을 반복해야 할 것입니다 l_linenumber = 1

관련 문제