我的数据看起来像:
如何按专业安排和分组数据。
我试过这样,但结果却是差异。格式。
var data = db.collection('hms').aggregate([
{ $match: { speciality: doctorSpeciality } },
{ $project: { doctor: 1, speciality: 1, meetting_hours: 1, hospital:1 } },
{
$group: {
"_id": "$doctor_speciality",
"speciality": {$push:"$speciality"},
"doctor":{$push :"$doctorSpeciality"},
"hospital":{$push : "$hospital_name" },
"meetting_hours":{$push: "$meetting_hours"}
}
}
]);
data.toArray(function(err, doc){
console.log("result :::" ,doc);
});
我想要这样的格式:
0:[
{ doctor:Hussain,speciality:Physician,hospital:HMS,date:2018-2-1},
{ doctor:Hussain,speciality:Physician,hospital:HMS,date:2018-2-2}
],
1: [{doctor:Reza,speciality:Physician,hospital:HMS,date:2018-2-1}]
等等。
我正在使用MongoClient驱动程序。
有人可以帮助我或给我一些提示吗?
答案 0 :(得分:1)
您只需要调整以立即推送所有值,而不是一次推送一个字段。
尝试
db.collection('hms').aggregate([
{"$match":{"speciality":doctorSpeciality}},
{"$project":{"doctor":1,"speciality":1,"meetting_hours":1,"hospital":1}},
{"$group":{
"_id":"$doctor_speciality",
"data":{
"$push":{
"speciality":"$speciality",
"doctor":"$doctor_speciality",
"hospital":"$hospital_name",
"meetting_hours":"$meetting_hours"
}
}
}}
])