为什么mongodb聚合如果过滤器不匹配,则返回空数组

时间:2019-01-13 08:14:24

标签: arrays mongodb filter nested aggregation

这是我的样本收藏。我想按费用过滤,通过匹配的查询获取价值。如果查询不匹配,则返回null。

[{
    "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" : 60000,
                        }, 
                        {
                            "program_name" : "Bachelor of Technology [B.Tech] (Electrical Engineering)",
                            "fees" : 40000,
                        }
                    ]
                },
                {
                    "course_type" : "Master of Technology [M.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Biotechnology)",
                            "fees" : 40000,
                        }, 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                        }
                    ]
                }
            ]
        }
    ]
}]

输出应为

[{
    "college_id" : 25,
    "name" : "IIT",
    "city" : "Delhi",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [ 
                {
                    "course_type" : "Master of Technology [M.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Biotechnology)",
                            "fees" : 40000,
                        }, 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                        }
                    ]
                }
            ]
        }
    ]
}]

我已经尝试使用$ match在下面的查询中,但是得到相同的输出。 我的查询是

[
  {
    "$project": {
    "name": 1,
    "city": 1,
      "courses": {
        "$map": {
          "input": {
            "$filter": {
              "input": "$courses",
              "as": "l1",
              "cond": {
                "$eq": [
                  "$$l1.discipline_name",
                  "engineering"
                ]
              }
            }
          },
          "as": "l2",
          "in": {
            "discipline_name": "$$l2.discipline_name",
            "course_list": {
              "$map": {
                "input": {
                  "$filter": {
                    "input": "$$l2.course_list",
                    "as": "l3",
                    "cond": [
                      // for course type filter
                    ]
                  }
                },
                "as": "l4",
                "in": {
                  "course_type": "$$l4.course_type",
                  "program_name_list": {
                    "$map": {
                      "input": {
                        "$filter": {
                          "input": "$$l4.program_name_list",
                          "as": "l5",
                          "cond": {
                            "$or": [
                              {
                                "$and": [
                                  {
                                    "$gt": [
                                      "$$l5.fees",
                                      0
                                    ]
                                  },
                                  {
                                    "$lt": [
                                      "$$l5.fees",
                                      200000
                                    ]
                                  }
                                ]
                              }
                            ]
                          }
                        }
                      },
                      "as": "l6",
                      "in": {
                        "program_name": "$$l6.program_name",
                        "fees": "$$l6.fees",
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }]

此查询的输出为

[{
    "name" : "IIT",
    "city" : "Delhi",
    "courses" : [ 
        {
            "discipline_name" : "engineering",
            "course_list" : [
                {
                    "course_type" : "Bachelor of Technology [B.Tech]",
                    "program_name_list" : [ 
                        // getting this null
                    ]
                },
                {
                    "course_type" : "Master of Technology [M.Tech]",
                    "program_name_list" : [ 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Biotechnology)",
                            "fees" : 40000,
                        }, 
                        {
                            "program_name" : "Master of Technology [M.Tech] (Electrical Engineering)",
                            "fees" : 20000,
                        }
                    ]
                }
            ]
        }
    ]
}]

我在$ project之后尝试了$ match,但是它不起作用。我也尝试过 $ match:{$ ne:[“ courses.course_list.program_name_list”,[]]}。它返回null。

0 个答案:

没有答案