2013-02-12 87 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