0

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) {

    }
Sona Shetty
  • 849
  • 1
  • 13
  • 32
  • Also see [Specify Multiple Conditions for Array of Documents](https://docs.mongodb.com/manual/tutorial/query-array-of-documents/#specify-multiple-conditions-for-array-of-documents) in the core documentation. `$elemMatch` is used to select the elements. You can use your separated match in order to actually "filter" the array elements. Or use `$filter` for them. – Neil Lunn Jun 10 '18 at 11:50
  • From above document your `"Treatment.name": "treatment2"` is not matching the condition... Or if you want to match mulitple criteria then use `$or` instead `$and` – Ashh Jun 10 '18 at 11:51

0 Answers0