2013-03-22 6 views
0

의 데이터를 조회하는 방법 : 나는 명령 쿼리를 사용했다MongoDB를

[{   
        "shop_ID" : "ABCD", 
        "plan" : 2, 
        "planType" : 2, 
        "goods_id" : "9612", 
        "category" : "1", 
        "keywords" : [ 
         { 
          "keyword" : "lianyiqun", 
          "price" : 3.12, 
          "score" : 9 
         }, 
         { 
          "keyword" : "nvzhuang", 
          "price" : 6.57, 
          "score" : 6 
         }, 
         { 
          "keyword" : "chunzhuang", 
          "price" : 5.55, 
          "score" : 8 
         } 
        ] 
       }, 
       { 
        "shop_ID" : "ABCD", 
        "plan" : 2, 
        "planType" : 2, 
        "goods_id" : "5078", 
        "category" : "1", 
        "keywords" : [ 
         { 
          "keyword" : "lianyiqun", 
          "price" : 9.26, 
          "score" : 8 
         }, 
         { 
          "keyword" : "nvzhuang", 
          "price" : 4.52, 
          "score" : 9 
         } 
        ] 
       }] 

을 :

db.test.find ({ "키워드": {$ elemMatch이 : { "점수": { "$ gte": 8}}}})

결과가 올바르지 않습니다. 모든 문서입니다.

그리고는 명령 db.test.find을 사용 ({ ". 키워드"점수 ": {"$ GTE "8}}).

결과는 동일

내가 원하는 제 문서 이런 쿼리 결과 :

이너 문서를 쿼리하는 방법
{   
         "shop_ID" : "ABCD", 
         "plan" : 2, 
         "planType" : 2, 
         "goods_id" : "9612", 
         "category" : "1", 
         "keywords" : [ 
          { 
           "keyword" : "lianyiqun", 
           "price" : 3.12, 
           "score" : 9 
          }, 
          { 
           "keyword" : "chunzhuang", 
           "price" : 5.55, 
           "score" : 8 
          } 
         ] 
        }, 

답변

0

각 어레이로부터 반환 것이다 $elemMatch 투영 오퍼레이터가 당신의 상태와 일치하는 요소 : 케이를

+0

감사를

db.test.aggregate ([ { $unwind: "$keywords" }, { $match: { "keywords.score" : { $gte: 8 } } } ]) 

감사합니다 :

db.test.find({ } , { keywords: { $elemMatch: { score: { $gte: 8 } } } } ) 

당신은 aggregation framework을 사용할 수, 점수> = 8로 하위 문서에있는 모든 요소를 ​​반환합니다. 그러나 mapreduce 작업에서이 기능을 사용하려면 db.collection.mapReduce() 함수가 집계를 지원하지 않는 또 다른 요구 사항이 있습니다. 내가 다음과 같이 쓴다면 : db.test.mapReduce ( mapFunction, reduceFunction, {out : "map_reduce_test", : {$ unwind : "$ keywords"}, {$ matches : { "keywords.score" : } ) 출력 감소 : { \t "errmsg": "예외 : 쿼리가 비어 있거나 개체 여야합니다." \t "코드": 13608, \t "ok": 0 – normalnotebook