查询具有内部数组的特定属性的集合

时间:2016-01-09 17:40:18

标签: mongodb database

我有一个名为“类别”的集合。我希望能找到一个标题为“Juices”的人。在定位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 
            }

1 个答案:

答案 0 :(得分: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 
    }
]

如果有人知道更好的方式,我很高兴听到它(好吧......看到它)