如何使用像这样的mongoose populate / ref?

时间:2016-05-21 02:22:15

标签: mongodb

我想找到每个传感器(根据sensor_id)信息。 sensor_id与_id相关。

我的代码是:

//this is vnodeInfo
      var vnodesSchema = new Schema({
                "_id": {
                    type: Schema.Types.ObjectId    //this is ref sensor_id
                },
                "node_id": {
                    type: String
                },
                "name": String,
                "sensors": [
                    {
                        "sensor_id": {
                            type: Schema.Types.ObjectId,
                            ref: "sensors"
                        },
                        "name": String,
                        "alias": String
                    }
                ]
            });



    //this is sensorsInfo
     var sensorsSchema = new Schema({
            "_id": {
                type: Schema.Types.ObjectId    //this is link
            },
            "node_id": {
                type: String,
                ref: "Nodes"
            },
            "order": String,
            "name": String,
            "alias": String,
            "low_limit": {
                type: Number
            },
            "high_limit": {
                type: Number
            },
            "value": {
                type: Number
            },
            "state": String,
            "last_update": {
                type: Date, default: Date.now
            }
        });

这是我的静力学方法: 我正在使用与sensorSchema相关的sensor.sensor_id " sensor_id == _ ID"使用此参考来获取传感器的信息

findSensorsById: function (vnodeId,callback) {  
    //use sensors.sensor_id to get sensorinfo  
    //this is a static method.
             return this.findById(vnodeId).populate('sensors.sensor_id').exec(callback)    
                    }

0 个答案:

没有答案