2014-10-11 129 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