查找嵌套数组元素

时间:2017-01-12 15:22:42

标签: node.js mongodb mongoose mongodb-query

这是我的架构:

<section class="prog__container">
 <span class="prog__sub">Title</span>
 <p>PEP 336 - Make None Callable</p>
 <span class="prog__sub">Description</span>
 <p>
 <p>
 <code>
      None
     </code>
     should be a callable object that when called with any
 arguments has no side effect and returns
     <code>
      None
     </code>
     .
    </p>
 </p>
 </section>

这是典型的家。

<section class="prog__container">\n <span class="prog__sub">Title</span>\n <p>PEP 336 - Make None Callable</p>\n <span class="prog__sub">Description</span>\n <p>\n <p>\n <code>\n      None\n     </code>\n     should be a callable object that when called with any\n arguments has no side effect and returns\n     <code>\n      None\n     </code>\n     .\n    </p>\n </p>\n </section>

soup = BeautifulSoup(html, "html.parser") for match in soup.findAll(['div']): match.unwrap() for match in soup.findAll(['a']): match.unwrap() html = soup.contents[0] html = str(html) html = html.splitlines(True) html = " ".join(html) html = re.sub(re.compile("\n"), "\\n", html) html = repl(html) # my current solution works, but unusable 有嵌套的房间数组,每个房间都有嵌套的设备数组。

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": [] } } 运营商不会工作。

是否可以接收该特定设备或仅返回该特定房间和设备的整个文档?

如何查找特定设备并退回该设备?

1 个答案:

答案 0 :(得分: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)
});