Mongodb搜索和过滤嵌套数组数据

时间:2018-12-21 14:13:02

标签: arrays mongodb search filter unique

我想过滤数组并在查询时获取所有结果

[{
    "college_id" : 25,
    "name" : "IIT",
    "city" : "Delhi",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [ 
                {
                    "course_type" : "Bachelor of Technology [B.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "48"
                        }, 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                            "duration" : "48"
                        }
                    ]
                },
                {
                    "course_type" : "Master of Technology [M.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "24"
                        }, 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                            "duration" : "24"
                        }
                    ]
                }
            ]
        }
    ]
},
{
    "college_id" : 75,
    "name" : "NIT",
    "city" : "Kolkata",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [ 
                {
                    "course_type" : "Bachelor of Technology [B.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "48"
                        }, 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                            "duration" : "48"
                        }
                    ]
                }
            ]
        },
        {
            "discipline_name" : "management",
            "course_list" : [ 
                {
                    "course_type" : "Master in Business Administration [MBA]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master in Business Administration [MBA] (Finance)",
                            "fees" : 40000,
                            "duration" : "48"
                        }, 
                        {
                            "program_name" : "Master in Business Administration [MBA] (Marketing)",
                            "fees" : 20000,
                            "duration" : "48"
                        }
                    ]
                }
            ]
        }
    ]
}]

问题: 1.查找费用大于30000的地方。 输出应为:

[{
    "college_id" : 25,
    "name" : "IIT",
    "city" : "Delhi",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [ 
                {
                    "course_type" : "Bachelor of Technology [B.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "48"
                        }
                    ]
                },
                {
                    "course_type" : "Master of Technology [M.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "24"
                        }
                    ]
                }
            ]
        }
    ]
},
{
    "college_id" : 75,
    "name" : "NIT",
    "city" : "Kolkata",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [ 
                {
                    "course_type" : "Bachelor of Technology [B.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Biotechnology)",
                            "fees" : 40000,
                            "duration" : "48"
                        }
                    ]
                }
            ]
        },
        {
            "discipline_name" : "management",
            "course_list" : [ 
                {
                    "course_type" : "Master in Business Administration [MBA]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master in Business Administration [MBA] (Finance)",
                            "fees" : 40000,
                            "duration" : "48"
                        }
                    ]
                }
            ]
        }
    ]
}]

2。查找费用大于30000并且小于50000的地方,“ course_type”是“工商管理硕士(MBA)” 样本输出

[{
    "college_id" : 75,
    "name" : "NIT",
    "city" : "Kolkata",
    "courses" : [ 
        {
            "discipline_name" : "management",
            "course_list" : [ 
                {
                    "course_type" : "Master in Business Administration [MBA]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master in Business Administration [MBA] (Finance)",
                            "fees" : 40000,
                            "duration" : "48"
                        }
                    ]
                }
            ]
        }
    ]
}]

0 个答案:

没有答案