" $匹配"在mongodb数组中没有返回预期的结果

时间:2018-06-10 11:42:00

标签: node.js mongodb mongoose mongoose-schema

我有一个像下面的MongoDB系列,用于存放医院,医院提供的治疗和提供特殊治疗的医生

{
    "_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
}

以下是用于检索提供特定治疗的医生名单的查询。但是,无论治疗名称如何,所使用的查询都返回注册号10003。

有人可以帮我纠正查询,以便根据治疗名称返回医生注册号

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

    }

0 个答案:

没有答案