简体   繁体   中英

Update deeply nested subdocument using mongoose

I tried to read most of the similar questions(and there are a lot) but couldn't find any solution I could understand.

The document structure as mentioned below-

[
  {RoomID:Room1,
  Boards:[
        {BoardID:Board1,
         Devices:[
         {DeviceID:Dev1}
         ....]
              }
              ....]
                   }
                   .....]

How to add a new Device after querying both RoomID and BoardID?

You can get the document with findOne() method. When you have the room object, you can finde the board with this call:

var board = room.Boards.find(board => board.BoardId === BoardID);

Once you have the board you can add it the device:

board.Devices.push(Device);

And finally you have to save the room object in db:

room.save();

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM