在数组Mongoose中搜索对象

时间:2017-01-06 14:21:06

标签: node.js mongodb angular mongoose mongodb-query

假设我有一个叫做部门的模型。我有一个房间阵列,每个房间都有病人(有病人)。

我想让患者的患者等于请求ID。有没有办法实现这个目标?

var department = mongoose.model('department', {
    departmentName: String,
    rooms: [
        {
            roomNumber: String,
            width: Number,
            height: Number,
            posX: Number,
            posY: Number,
            sanitary: Boolean,
            childcareArea: Boolean,
            lounge: Boolean,
            patient: {
                patientnr: Number,
                firstname: String,
                lastname: String,
                reasonOfHospitalization: String,
                dateOfHospitalization: Date,
                expectedDateOfDischarge: Date,
                vegetarian: Boolean,
                needsHelp: Boolean,
                comments: String,
                department: String,
                roomNumber: String,
                nextTreatment: {
                    type: String,
                    shortDescription: String,
                    timestamp: Date
                }
            }
        }
    ]
});

//get patient with id
app.get('/api/departments/patients/:id', function(req, res) {
    department.find({"patient": {$all :[60022]}}, function (err, patient) {
        if (err)
            res.send(err)
        res.json(patient);
    });
});

2 个答案:

答案 0 :(得分:1)

您可以在投影中使用 $elemMatch ,如下所示:

//get patient with id
app.get('/api/departments/patients/:id', function(req, res) {
    department.findOne(
        { "rooms.patient.patientnr": parseInt(req.params.id) }, 
        { 
            "rooms": {
                "$elemMatch": {
                    "patient.patientnr": parseInt(req.params.id)
                }
            }
        },
        function (err, dept) {
            if (err)
                res.send(err)
            var patient = dept.rooms[0].patient;
            res.json(patient);
        }
    );
});

$ positional operator

//get patient with id
app.get('/api/departments/patients/:id', function(req, res) {
    department.findOne(
        { "rooms.patient.patientnr": parseInt(req.params.id) }, 
        { "rooms.$": 1 },
        function (err, dept) {
            if (err)
                res.send(err)
            var patient = dept.rooms[0].patient;
            res.json(patient);
        }
    );
});

或使用聚合框架:

//get patient with id
app.get('/api/departments/patients/:id', function(req, res) {
    department.aggregate(
        [
            { "$match": { "rooms.patient.patientnr": parseInt(req.params.id) } },
            {
                "$project": {
                    "room": {
                        "$arrayElemAt": [
                            {
                                "$filter": {
                                    "input": "$rooms",
                                    "as": "room",
                                    "cond": {
                                        "$eq": [
                                            "$$room.patient.patientnr", 
                                            parseInt(req.params.id)
                                        ]
                                    }
                                }
                            }, 0
                        ]
                    }
                }
            }
        ],  function (err, results) {
            if (err)
                res.send(err)
            var patient = results[0].room.patient;
            res.json(patient);
        }
    );
});

答案 1 :(得分:0)

你可以试试这个

//get patient with id
app.get('/api/departments/patients/:id', function(req, res) {
    var id = req.params.id;
    department.find({"rooms.patient.patientnr": id}, function (err, patient) {
        if (err)
            res.send(err)
        res.json(patient);
    });
});