查询mongodb中特定键的所有文档和子文档,无需循环

时间:2013-04-19 16:23:53

标签: json mongodb

我很难为这个问题选择合适的标题。我有一份文件和子文件清单如下

{
        "name" : "Pets",
        "subcategories" : [
            {
                "name" : "Cats",
                "fbCategories" : []
            },
            {
                "name" : "Dogs",
                "fbCategories" : []
            }
        ],
        "fbCategories" : ["Pet Services","Pet Supplies", "Animal"]
    },
    {
        "name" : "Entertainment",
        "subcategories" : [
            {
                "name" : "Music",
                "subcategories" : [
                    {
                        "name" : "Albums",
                        "fbCategories" : [ "Album"]
                    },
                    {
                        "name" : "Musicians",
                        "fbCategories" : ["Concert Tour","Musician/Band"]
                    }
                ],
                "fbCategories" : [
                    "Concert Venue",
                    "Music Chart",
                    "Music Video",
                    "Musical Instrument",
                    "Record Label",
                    "Music"
                ]
            }

在此示例中,我们有两个主要类别 (“Pets”,“Entertainment”) 及其相应的子类别(“宠物”的猫和狗) ,...)。虽然有不同级别的子类别,但所有文档/子文档都有两个键, name和fbCategories 。我想在不编写循环或多个查询的情况下找到每个类别/子类别的fbCategories。无论如何在一个查询中查询它们,结果应列出每个“name”键的“fbCategories”。例如,对于“musicians”,它会列出["Concert Tour","Musician/Band"],而对于“音乐”,它会列出

 ["Concert Venue","Music Chart","Music Video","Musical Instrument","Record Label","Music"]          

1 个答案:

答案 0 :(得分:0)

请看:Finding embeded document in mongodb?

没有循环,imho,这个问题还没解决,因为$ where语句限制在聚合框架中。