I have a MongoDB collection like below which store the hospital, treatments offered by hospital and doctors offering a particular treatment
{
"_id": {
"$oid": "5aefac65a40b1d39d3344cad"
},
"hospitaldisplayname": "My name",
"hospitalDescription": "my path",
"hospitalName": "hospital name",
"hospitalID": 10015,
"updated_at": {
"$date": "2018-05-07T01:31:17.075Z"
},
"Treatment": [
{
"treatmentdisplayname": "treatment1",
"name": "treatment1",
"departmentName": "Ayurveda",
"_id": {
"$oid": "5aefac65a40b1dbb89344caf"
},
"doctor": [
{
"registrationNumber": "10003",
"registrationAuthority": "AMAI",
"_id": {
"$oid": "5aefac65a40b1d1e55344cb0"
},
"activeFlag": "Y"
}
],
"activeFlag": "Y"
},
{
"treatmentdisplayname": "treatment2",
"name": "treatment2",
"departmentName": "Ayurveda",
"_id": {
"$oid": "5aefac65a40b1dbb89344caf"
},
"doctor": [
{
"registrationNumber": "10008",
"registrationAuthority": "AMAI",
"_id": {
"$oid": "5aefac65a40b1d1e55344cb0"
},
"activeFlag": "Y"
}
],
"activeFlag": "Y"
},
],
"serviceActiveFlag": "Y",
"__v": 0
}
Below is the query used to retrive list of doctors offering a particular treatment.But the query used is returning registration number 10003 irrespective of the treatment name.
Can someone help me to correct the query so that it will return doctor registration number based on the treatment name
myModel.aggregate([
{
"$match": { "$and": [{ "serviceActiveFlag": "Y" }, { "Treatment.name": "treatment2" }, { "Treatment.doctor.activeFlag": { $in: ["Y"] } }] }
},
{ $unwind: "$Treatment" },
{ $unwind: "$Treatment.doctor" },
{
$project: {
"docregistrationnumber":"$Treatment.doctor.registrationNumber",
"docregistrationauthority": "$Treatment.doctor.registrationAuthority",
}
}
], function (err, result) {
}