title: Mongodb: How to change the value of a nested document using mongodb java driver
body: <p>My document looks something like this:</p>
  
  <pre><code>{
  "_id" : ObjectId("5a5081321d5e182e14f20589"),
  "building_number" : "01",
  "floor_1" : {
      "roomA" : {
          "occupant" : "vacant",
          "department" : "science",
          "space_code" : "103",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "8076809513456",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "9780444505156",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "5209533250890",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "9303939104545",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      },
      "roomB" : {
          "occupant" : "vacant",
          "department" : "science",
          "space_code" : "103",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "7258029188960",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "4219366494100",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "985355502553",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "930393910527",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "93039391033",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      },
      "roomC" : {
          "occupant" : "vacant",
          "department" : "science",
          "space_code" : "103",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "7258021234960",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "4219366741236",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "985355509513",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "987593910527",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      }
  },
  "floor_2" : {
      "roomA" : {
          "occupant" : "vacant",
          "department" : "mathematics",
          "space_code" : "105",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "7776809513456",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "9120444505156",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "4209533250890",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "9303939104545",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      },
      "roomB" : {
          "occupant" : "vacant",
          "department" : "mathematics",
          "space_code" : "105",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "3658029188960",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "2119366494100",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "495355502553",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "930393910527",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "46039391033",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      },
      "roomC" : {
          "occupant" : "vacant",
          "department" : "mathematics",
          "space_code" : "105",
          "space_type" : "office",
          "furniture_assets" : {
              "number_1" : {
                  "barcode" : "7258074124960",
                  "description" : "Metal Desk",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_2" : {
                  "barcode" : "4219367896236",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_3" : {
                  "barcode" : "22535512313",
                  "description" : "Pine bookcase",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              },
              "number_4" : {
                  "barcode" : "142593910527",
                  "description" : "Gas-lift Chair",
                  "acquisition_date" : "07/07/2008",
                  "cost" : "$100"
              }
          }
      }
    }
  
  
  }
  </code></pre>
  
  <p>How do I change the object vacant using the java driver?</p>
  
  <p>I used the code below but it didn't seem to work.</p>
  
  <pre><code>  DBCollection coll = db.getCollection("Facilities");
          String buildingNum = (String) buildingBox.getValue();
          String floorNum = (String) floorBox.getValue();
          String roomNum = (String) roomBox.getValue();
          BasicDBObject document = new BasicDBObject();
          document.put("building_number", buildingNum);
          DBCursor dbCursor = coll.find(document);
          while(dbCursor.hasNext()) {
             DBObject dbo =  dbCursor.next();
          //DBObject  dbObject1 = (DBObject) (((DBObject)  dbo.get(floorNum)).get(roomNum));
           //  String change =  floorNum;
          String email = userDetails.get(0);
           BasicDBObject occupant = new BasicDBObject();
  
              occupant.append("$set",new BasicDBObject().append( "occupant",email));
           DBObject findRoom = (DBObject) ((DBObject) dbo.get(floorNum)).get(roomNum);
  
           coll.update(findRoom,occupant,false,false);
  </code></pre>
  
  <p>Even though there are no errors it won't change the value in the database. So how do I update the value "occupant" using the java driver?
  Is there a way to change the value? Like an adaptation of this code?</p>