2013-02-12 86 views
0

数组元素的,我有以下的JSON文件:投影使用MongoDB的

{ 
    "Section": { 
     "Name": "Section 1", 
     "Description": "Section 1 information", 
     "Icon": "test.png" 
    }, 
    "Data": [{ 
     "Name": "Firstname", 
     "Desc": "Users first name", 
     "Type": "Text" 
    }, { 
     "Name": "Lastname", 
     "Desc": "Users last name", 
     "Type": "Text" 
    }] 
} { 
    "Section": { 
     "Name": "Section 2", 
     "Description": "Section 2 information", 
     "Icon": "test.png" 
    }, 
    "Data": [{ 
     "Name": "Section 2 Item", 
     "Desc": "Section 2 Desc", 
     "Type": "Text" 
    }] 
} 

我想问问有没有什么办法可以投射基于Data.Name项目。像['Firstname','Section 2 Item']中的Data.Name项目一样,包括Section对象。所以结果应该是:

{ 
    "Section": { 
     "Name": "Section 1", 
     "Description": "Section 1 information", 
     "Icon": "test.png" 
    }, 
    "Data": [{ 
     "Name": "Firstname", 
     "Desc": "Users first name", 
     "Type": "Text" 
    }] 
} { 
    "Section": { 
     "Name": "Section 2", 
     "Description": "Section 2 information", 
     "Icon": "test.png" 
    }, 
    "Data": [{ 
     "Name": "Section 2 Item", 
     "Desc": "Section 2 Desc", 
     "Type": "Text" 
    }] 
} 

不知道这是否可能。

在此先感谢。

回答

3

您可以识别的查询对象相匹配的数组元素的索引$位置投影算这样做:

db.test.find(
    {'Data.Name': {$in: ["Firstname", "Section 2 Item"]}}, 
    {Section: 1, 'Data.$': 1}) 
+0

我敢肯定,我想,在我张贴了我的问题。无论如何,它的工作原理与我想要的一样。非常感谢。 – Yannis 2013-02-12 16:53:24