2017-05-21 1 views
0

이하 json은 내 컬렉션의 문서 구조를 나타냅니다.MongoDB의 배열 열을 제외 할 쿼리

{ 
    "_id" : ObjectId("591a653c366df19100ed0fbc"), 
    "sno" : 1, 
    "chapterName" : "chapter 1: Express JS", 
    "overView" : "Overview of Express JS", 
    "sections" : [ 
      { 
        "title" : "The Node.js philosophy", 
        "subSections" : [ 
          { 
            "sno" : 1, 
            "title" : "Small core 1", 
            "content" : "The Node.js core itself has its foundations 1" 
          }, 
          { 
            "sno" : 2, 
            "title" : "Small core 2", 
            "content" : "The Node.js core itself has its foundations 2" 
          } 
        ] 
      }, 
      { 
        "title" : "The Node.js philosophy 2", 
        "subSections" : [ 
          { 
            "sno" : 1, 
            "title" : "Small core 1", 
            "content" : "The Node.js core itself has its foundations 1" 
          }, 
          { 
            "sno" : 2, 
            "title" : "Small core 2", 
            "content" : "The Node.js core itself has its foundations 2" 
          } 
        ] 
      } 
    ] 
} 
("내용"제외) 내가 아래에 언급 된 방법으로 모든 레코드에 대한 데이터를 반환하는 쿼리를 작성하고 싶습니다

{ 
    "_id" : ObjectId("591a653c366df19100ed0fbc"), 
    "sno" : 1, 
    "chapterName" : "chapter 1: Express JS", 
    "overView" : "Overview of Express JS", 
    "sections" : [ 
      { 
        "title" : "The Node.js philosophy", 
        "subSections" : [ 
          { 
            "sno" : 1, 
            "title" : "Small core 1" 

          }, 
          { 
            "sno" : 2, 
            "title" : "Small core 2" 

          } 
        ] 
      }, 
      { 
        "title" : "The Node.js philosophy 2", 
        "subSections" : [ 
          { 
            "sno" : 1, 
            "title" : "Small core 1" 

          }, 
          { 
            "sno" : 2, 
            "title" : "Small core 2" 

          } 
        ] 
      } 
    ] 
} 

어떤 생각이 어떻게 실현하려?

+1

가능합니다. 하지만 배열을 중첩하지 않아야합니다. 배열을 업데이트하는 방법에 제한이 있습니다. 따라서 가능한 한 쉽지는 않지만 문서 구조를 조금만 편다면 훨씬 간단하고 효율적입니다. 하나의 배열을 다른 배열에 중첩하지 않고 단일 배열의 항목에 대한 속성을 생각하십시오. –

답변

0

그렇게 할 수 있지만 "사소하지 않음".

:이 .aggregate()

db.collection.aggregate([ 
    { "$project": { 
     "sno": 1, 
     "chapterName": 1, 
     "overView": 1, 
     "sections": { 
     "$map": { 
      "input": "$sections", 
      "as": "section", 
      "in": { 
      "title": "$$section.title", 
      "subSections": { 
       "$map": { 
       "input": "$$section.subSections", 
       "as": "subSection", 
       "in": { 
        "sno": "$$subSection.sno", 
        "title": "$$subSection.title" 
       } 
       } 
      } 
      } 
     } 
     } 
    }} 
]) 

난 그냥 생각 "병합"에 대해 언급을 만들고 싶어하고, 중첩 된 배열을 할 수없는 이유의 다른 모든 고려 사항 외부에서 간단하게이 데이터를 고려와 $map 많이 사용을 필요로 그때 내가 할 필요를 "c" 속성을 제외 할 경우 그 이후

{ 
    "_id" : ObjectId("5921745d13fbc408ee20a60a"), 
    "a" : 1, 
    "b" : [ 
    { "a" : 1, "b" : 2, "c" : 3 }, 
    { "a" : 4, "b" : 5, "c" : 6 } 
    ] 
} 

는 단 하나 개의 배열 수준 깊은입니다입니다 :

db.collection.find({},{ "b.c": 0 }) 

와 나는 물론 얻을 :

{ 
    "_id" : ObjectId("5921745d13fbc408ee20a60a"), 
    "a" : 1, 
    "b" : [ 
    { "a" : 1, "b" : 2 }, 
    { "a" : 4, "b" : 5 } 
    ] 
} 

를 그리고 명확하게 집계 프로젝션 $map 문을 중첩보다 훨씬 간단합니다.

생각을위한 음식.