2017-01-12 8 views
0

を探すこれは私のスキーマです:ネストされた配列要素に

Appliance = new Schema({ 
    appliance_name: {type:String}, 
    appliance_id: {type:String}, 
    appliance_description: {type:String}, 
    keywords: [{ type: String}], 
    appliance_type: { type: String}, 
    appliance_status: { light: { write_state: Number, read_state: Number }, 
        fan: {write_state: Number, read_state: Number, write_speed: Number, read_speed: Number} 
    } 

}); 

Room= new Schema({ 
    name: {type: String, required:true}, 
    device_auth_code: {type: String}, 

    alt_name: {type:String}, 
    keywords: [{type: String}], 
    appliance: [Appliance] 
}); 

Home = new Schema({ 
    name: { type: String, required: true}, 
    description: {type: String}, 
    administrator: {type : mongoose.Schema.Types.ObjectId, ref: 'User', required: true}, 

    users: [{ 
     _id: {type : mongoose.Schema.Types.ObjectId, ref: 'User'}, 
     email: {type: String}, 
     name: { type: String}, 
     status: { type: Number} 
    }], 
    rooms: [Room] 


}); 

そして、ここでは代表的な家です。

"type": true, 
    "code": "GET_SUCCESS", 
    "homes": { 
    "_id": "58760ff6045e332b81449b42", 
    "description": "", 
    "administrator": "586df1e06485de5fc48b72a5", 
    "name": "CM", 
    "__v": 9, 
    "rooms": [ 

     { 
     "name": "RK", 
     "alt_name": "RKa", 
     "_id": "58775437234451346ce3d967", 
     "appliance": [ 
      { 
      "_id": "5877546f234451346ce3d968", 
      "appliance_type": "Light", 
      "appliance_name": "TubeLights", 
      "keywords": [] 
      } 
     ], 
     "keywords": [] 
     } 
    ], 
    "users": [] 
    } 
} 

Homeには、ネストされた部屋の配列があり、各部屋にはアプライアンスのネストされた配列があります。

Home.findOne({'room.appliance._id': appliance_id})は、ドキュメント全体を返します。 $の演算子は動作しませんでした私は想像する必要があります。

特定のアプライアンスまたはその特定のルームとアプライアンスのみが返されるドキュメント全体を受信することは可能ですか?

特定のアプライアンスを見つけて返すにはどうすればよいですか?

答えて

0

ここでは、ホームスキーマ定義でmongoose subdocsアーキテクチャを使用しました。だから、任意の特定の部屋を取得することができます:

Home.findById(home_id, function(err, home){ 
    var room = home.rooms.id(room_id); 
    res.json(room) 
}); 

または特定のアプライアンス:

Home.findById(home_id, function(err, home){ 
    var room = home.rooms.id(room_id); 
    var appliance = room.appliance.id(appliance_id); 
    res.json(appliance) 
}); 
関連する問題