英文:
MongoDb aggregation for filtering list based on ids present in object of array from all document of same collection
问题
在我的mongodb中,数据如下所示:
{
  "studentId": "a1",
  "name": "John Doe",
  "studentsReferred": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2"
        },
        {
          "studentId": "a3"
        }
      ]
    },
	{
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4"
        },
        {
          "studentId": "a5"
        }
      ]
    }
  ]
},
{
  "studentId": "a2",
  "name": "Joseph",
  "studentsReferred": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a6"
        }
      ]
    }
  ]
}
上述JSON包含MongoDB集合中的文档。每个文档包含学生的所有详细信息以及推荐详情摘要。即,对于每个学生,都有一个包含由该学生引荐的所有学生的字段studentsReferred。
我想在检索单个学生时显示该学生的所有详细信息以及被引荐的学生的姓名,如下所示:
{
  "studentId": "a1",
  "name": "John Doe",
  "studentsReferred": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2",
		  "name": "Joseph"
        },
        {
          "studentId": "a3",
		  "name": "Lorem Ipsum"
        }
      ]
    },
	{
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4",
		  "name": "Lorem Ipsum"
        },
        {
          "studentId": "a5",
		  "name": "Lorem Ipsum"
        }
      ]
    }
  ]
}
我尝试使用MongoDB聚合来解决这个问题。但不幸的是,我无法为此编写查询。所以我们可以使用聚合来实现上述情景吗?
英文:
In my mongodb I have the data as shown below:
{
  "studentId": "a1",
  "name":"John Doe"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2"
        },
        {
          "studentId": "a3"
        }
      ]
    },
	{
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4"
        },
        {
          "studentId": "a5"
        }
      ]
    }
  ]
},
{
  "studentId": "a2",
  "name":"Joseph"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a6"
        }
      ]
    }
  ]
}
Above JSON contains documents in collection of MongoDB. Each document contains all details of student along with referral detail summary. i.e. for every student there is a field studentsReferred which contain ids of all students which are referred by the student.
I want to show all details of student alone with the name of students which are reffered while retrieving the single student. As below
{
  "studentId": "a1",
  "name":"John Doe"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2",
		  "name":"Joseph"
        },
        {
          "studentId": "a3",
		  "name":"Lorem Ipsum"
        }
      ]
    },
	{
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4",
		  "name":"Lorem Ipsum"
        },
        {
          "studentId": "a5",
		  "name":"Lorem Ipsum"
        }
      ]
    }
  ]
}
I have tried to use mongodb aggregation for this problem. But unfortunately I am not able write query for that. So can we achieve the above scenario using aggregation.
答案1
得分: 2
你可以尝试以下操作:
- 使用
$facet创建两个数组,users包含用户详细信息的姓名和学生ID,第二个数组是所有用户的详细信息allUsers。 - 使用 
$project进行迭代循环- 使用 
$map处理allUsers数组作为输入 - 使用 
$map处理studentsReffered数组作为输入 - 使用 
$map处理students数组作为输入 - 使用 
$reduce在条件匹配时从users数组中获取学生数据 
 - 使用 
 - 使用 
$unwind展开allUsers数组 - 使用 
$replaceWith将allUsers对象替换为根对象 
db.collection.aggregate([
  {
    $facet: {
      users: [
        {
          $project: {
            studentId: 1,
            name: 1
            // 添加所需的字段,它们会自动反映在连接中
          }
        }
      ],
      allUsers: []
    }
  },
  {
    $project: {
      allUsers: {
        $map: {
          input: "$allUsers",
          in: {
            $mergeObjects: [
              "$$this",
              {
                studentsReferred: {
                  $map: {
                    input: "$$this.studentsReferred",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        {
                          students: {
                            $map: {
                              input: "$$this.students",
                              as: "s",
                              in: {
                                $reduce: {
                                  input: "$users",
                                  initialValue: { studentId: "$$s.studentId" },
                                  in: {
                                    $cond: [
                                      { $eq: ["$$this.studentId", "$$s.studentId"] },
                                      "$$this",
                                      "$$value"
                                    ]
                                  }
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  { $unwind: "$allUsers" },
  { $replaceWith: "$allUsers" }
])
英文:
You can try,
$facetto create 2 arrays,usersuser details name and studentId, second all users details inallUsers$projectiterate loop$mapinput as allUsers array$mapinput as studentsReffered array$mapinput as students array$reduceto get data of the student fromusersarray when condition match
$unwinddeconstruct allUsers array$replaceWithreplace allUsers object in root
db.collection.aggregate([
  {
    $facet: {
      users: [
        {
          $project: {
            studentId: 1,
            name: 1
            // add fields as you want it will automatically reflect in join
          }
        }
      ],
      allUsers: []
    }
  },
  {
    $project: {
      allUsers: {
        $map: {
          input: "$allUsers",
          in: {
            $mergeObjects: [
              "$$this",
              {
                studentsReffered: {
                  $map: {
                    input: "$$this.studentsReffered",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        {
                          students: {
                            $map: {
                              input: "$$this.students",
                              as: "s",
                              in: {
                                $reduce: {
                                  input: "$users",
                                  initialValue: { studentId: "$$s.studentId" },
                                  in: {
                                    $cond: [
                                      { $eq: ["$$this.studentId", "$$s.studentId"] },
                                      "$$this",
                                      "$$value"
                                    ]
                                  }
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  { $unwind: "$allUsers" },
  { $replaceWith: "$allUsers" }
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论