Group by调用另一个函数,根据golang mongodb中的Group by元素对文档进行计数
我使用golang作为Mongodb的后端 我的mongodb收藏是Group by调用另一个函数,根据golang mongodb中的Group by元素对文档进行计数,mongodb,go,Mongodb,Go,我使用golang作为Mongodb的后端 我的mongodb收藏是 **employee** { _id:ObjectId(), "emp_name":"qwert", "emp_id":111, "emp_dept":"XYZ" "qualification":"PHD", "employee_status"
**employee**
{
_id:ObjectId(),
"emp_name":"qwert",
"emp_id":111,
"emp_dept":"XYZ"
"qualification":"PHD",
"employee_status":"working"
}
{
_id:ObjectId(),
"emp_name":"asdfg",
"emp_id":121,
"emp_dept":"XYZ"
"qualification":"MBA"
"employee_status":"working"
}
**department**
{
_id:ObjectId(),
"dept_id":11,
"dept_name":"XYZ",
"description":"decs",
"department_status":"active"
}
我的密码是
type DepartmentInfo struct {
DepartmentID int `json:"dept_id" bson:"dept_id"`
DepartmentName string `json:"dept_name" bson:"dept_name"`
Description string `json:"description" bson:"description"`
EmployeeCount int `json:"employee_count" bson:"employee_count"`
}
collection := session.DB("db").C("employee")
pipeline := collection.Pipe([]bson.M{
{"$match": bson.M{ "qualification": "PHD", }},
{"$group": bson.M{"_id": "$emp_dept",
"total_employee": bson.M{"$sum": GetDepartmentEmployeeCount("$emp_dept")}, }},
{"$lookup": bson.M{
"from": "department",
"localField": "_id",
"foreignField": "dept_name",
"as": "department_info",
}},
{"$unwind": "$department_info"},
{"$match": bson.M{"$or": []interface{}{
bson.M{"department_info.status": bson.M{"$exists": false}},
bson.M{"department_info.status": 1}, // department 1 means active
}}},
})
var departmentInfo [] Department
err = pipeline.All(&departmentInfo)
其他功能为
func GetDepartmentEmployeeCount(var interface{})(int res)
{
......
return res
}
当我在
GetDepartmentEmployeeCount
函数中获得emp\u dept
时,它会像字符串一样打印它,而不是像emp\u dept
那样获取值。我想执行类似select count(*)的查询从employee where emp_dept='var'
。我的问题是如何让另一个函数中的group by元素执行查询?能否澄清您希望看到的输出?你现在得到了什么?