2016-01-09 109 views
0

我有一個名爲「categories」的集合。我希望能夠找到一個標題爲「果汁」的作品。在定位Juices之後,我希望能夠看到其中的所有項目。查詢具有內部數組的特定屬性的集合

{ 
    "_id": "xibspjSNQLJKurHoT", 
    "title": "Juices", 
    "description": "all types of juices", 
    "items": [ 
     { 
      "title": "Apple Juice", 
      "description": "Made with apples", 
      "healthy": true 
     }, 
     { 
      "title": "Orange Juice", 
      "description": "Made with oranges", 
      "healthy": true 
     } 
    ], 
    "author": "9MJh7d4ELJ9guChvs", 
    "createdAt": ISODate("2016-01-09T05:03:49.681Z") 
}, 
{ 
    "_id": "xibspjSNQLJKurHoT", 
    "title": "Meats", 
    "description": "beef", 
    "items": [ 
     { 
      "title": "Chicken", 
      "description": "Made with chicken", 
      "healthy": true 
     } 
    ], 
"author": "9MJh7d4ELJ9guChvs", 
"createdAt": ISODate("2016-01-09T05:03:49.681Z") 

}

我想看到的結果是這樣的

  { 
       "title": "Apple Juice", 
       "description": "Made with apples", 
       "healthy": true 
      }, 
      { 
       "title": "Orange Juice", 
       "description": "Made with oranges", 
       "healthy": true 
      } 
+0

你說的*我要訪問一個名爲「項」 *數組是什麼意思?你能展示預期的結果嗎? – styvane

+0

我增加了更多的細節,我想回來的結果。我是使用嵌入式數據模型的新手。 – Jayswager

+0

可能的重複[如何在MongoDB中選擇單個字段?](http://stackoverflow.com/questions/25589113/how-to-select-a-single-field-in-mongodb) – styvane

回答

0

這是給我我想要的結果,在未來萬一有人找類似的東西這個。

var Juices = db.Categories.findOne({title: "Juices"}); 
    var items = Juices.items // gives you the results of 
[ 
    { 
     "title": "Apple Juice", 
     "description": "Made with apples", 
     "healthy": true 
    }, 
    { 
     "title": "Orange Juice", 
     "description": "Made with oranges", 
     "healthy": true 
    } 
] 

如果有人知道更好的方法,我很高興聽到它(以及..看到它)

+0

最好的方法是做這個服務器端。檢查可能重複的鏈接。 'db.Categories.findOne({'title':'Juices'},{'items':1,'_id':0})' – styvane

相關問題