2014-10-11 235 views
0

假設我在MongoDB中有一個像下面這樣的JSON對象作爲集合中的一個項目。搜索MongoDB嵌套樹對象

data:{ 
name:"newData", 
items: 
     [ 
     { 
     "id": 1, 
     "title": "1. dragon-breath", 
     "items": [] 
     }, 
     { 
     "id": 2, 
     "items": [ 
      { 
      "id": 21, 
      "title": "2.1. tofu-animation", 
      "items": [ 
       { 
       "id": 211, 
       "title": "2.1.1. spooky-giraffe", 
       "items": [] 
       }, 
       { 
       "id": 212, 
       "items": [] 
       } 
      ] 
      }, 
      { 
      "id": 22, 
      "title": "2.2. barehand-atomsplitting", 
      "items": [] 
      } 
     ] 
     }, 
     { 
     "id": 3, 
     "title": "3. unicorn-zapper", 
     "items": [ 
      { 
      "id": 30, 
      "title": "3. unicorn-zapper.1", 
      "items": [ 
       { 
       "id": 300, 
       "title": "3. unicorn-zapper.1.1", 
       "items": [ 
        { 
        "id": 3000, 
        "title": "3. unicorn-zapper.1.1.1", 
        "items": [ 
         { 
         "id": 30000, 
         "title": "3. unicorn-zapper.1.1.1.1", 
         "items": [ 
          { 
          "id": 300000, 
          "title": "3. unicorn-zapper.1.1.1.1.1", 
          "items": [] 
          } 
         ] 
         } 
        ] 
        } 
       ] 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "id": 4, 
     "title": "4. romantic-transclusion", 
     "items": [] 
     } 
    ] 

它是一個連續的樹結構,所有項目不一定需要具有「標題」屬性。如何在整個結構中搜索標題爲例如{「title」:「3。unicorn-zapper.1.1.1」}的項目並僅返回該項目。有任何想法嗎。

+4

這不是外包門戶,在那裏你轉儲你的工作,其他人爲你做。顯示一些功能,然後尋求幫助 – 2014-10-11 06:25:53

回答

0

從MongoDB 2.6.5開始,沒有辦法搜索超過一個深度的數組,除了通過使用點符號來搜索索引。即便如此,也沒有辦法爲多於一個深度的數組僅投影匹配文檔的匹配數組元素。我認爲你沒有考慮在你構建它們之前如何查詢你的文檔。如果您的數據中包含嵌套/樹狀結構,請通過data modeling docs on trees瞭解有關如何更好地模擬這種情況的建議,而不是使用任意嵌套的數組。

+0

謝謝@wdberkeley。我無法控制數據。但我已經通過遞歸for循環(C#驅動程序)來找到項目,但我認爲應該有更好的方法,因爲MongoDB是爲此目的而構建的。 – Paul 2014-10-14 00:15:49