почему агрегат mongodb возвращает пустой массив, если не совпадает с фильтром - PullRequest
0 голосов
/ 13 января 2019

Это моя коллекция образцов. Я хочу, чтобы отфильтровать по комиссиям, получая значение с соответствующим запросом. если запрос не совпадает, он возвращает нуль.

[{
    "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,
                        }
                    ]
                }
            ]
        }
    ]
}]

Я пробовал $ match после $ project, но он не работает. я тоже пытался $ match: {$ ne: ["courses.course_list.program_name_list", []]}. возвращает ноль.

...