2017-05-21 96 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

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

由於這只是一個陣列級深,如果我想排除"c"屬性,然後我需要做的是:

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

我也得到課程:

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

而這顯然比在聚合投影嵌套$map語句簡單得多。

思考的食物。