我的 mongodb 收藏
employee
{
_id:ObjectId(),
"emp_name":"qwert",
"emp_id":111,
"emp_dept":"XYZ"
"qualification":"PHD"
}
{
_id:ObjectId(),
"emp_name":"asdfg",
"emp_id":121,
"emp_dept":"XYZ"
"qualification":"MBA"
}
department{
_id:ObjectId(),
"dept_id":11,
"dept_name":"XYZ",
"description":"decs",
}
我的 Go 代码是
type Employee struct {
EmployeeName string `json:"emp_name" bson:"emp_name"`
EmployeeID int `json:"emp_id" bson:"emp_id"`
EmployeeDept string `json:"emp_dept" bson:"emp_dept"`
EmpQualification string `json:"qualification" bson:"quaification"`
EmpDepartment Department `json:"department" bson:"department"`
}
type Department struct {
DepartmentID int `json:"dept_id" bson:"dept_id"`
DepartmentName string `json:"dept_name" bson:"dept_name"`
Description string `json:"description" bson:"description"`
EmployeeList []Employee `json:"employee_list" bson:"employee_list"`
}
collection := session.DB("db").C("department")
pipeline := collection.Pipe([]bson.M{
{"$lookup": bson.M{
"from": "employee",
"localField": "dept_name",
"foreignField": "emp_dept",
"as": "employee_list",
}},
{"$match": bson.M{
"qualification": "PHD",
}},
})
err = pipeline.All(&departmentEmployees)
它显示空结果
我想从我的部门集合中获取所有部门,每个部门都有“PHD”资格的员工列表。我已将我的集合和结构上传为示例。如何在查找表中使用匹配字段。
我想得到像
{
dept_id:11,
dept_name:'XYZ'
description:'desc'
employee_list:[
{
emp_name:"qwerty"
emp_id:111,
qualification:'PHD'
}
{
emp_name:"asdfg"
emp_id:222,
qualification:'PHD'
}
...
]}
{
dept_id:12,
dept_name:'ABC'
description:'descwe'
employee_list:[
{
emp_name:"bjgk"
emp_id:865,
qualification:'PHD'
}
{
emp_name:"hkj"
emp_id:967,
qualification:'PHD'
}
...
]}
对于第一所大学,让所有员工获得博士学位,然后在第二所大学获得博士学位,依此类推。我很困惑我是否必须在查找中使用 group by in 从集合中?
慕容3067478
相关分类