title: Insert a document into another document in MongoDB
body: <p>I'm trying to add a document in another document. </p>
  
  <p><a href="https://i.stack.imgur.com/oedGd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oedGd.png" alt="enter image description here"></a></p>
  
  <p>I am trying to insert a new document with a timestamp as a key and light prox and temp as the content of that document into document <code>sensor_collection</code>.</p>
  
  <p>It's logical that my code doesn't work, because I'm setting a new <code>sensor_collection</code>. Does anyone know how I can set a timestamp document in <code>sensor_collection</code> or is it adviced not to do it this way?</p>
  
  <p>This is the code:</p>
  
  <pre><code>MongoCollection&lt;Document&gt; collection  =  db.getCollection(Sensor.KEY_COLLECTION);
      //append sensor data to existing document
      collection.updateOne(doc, new Document("$set",
              new Document("sensor_collection", new Document(
                      String.valueOf(stamp.getCurrentTime()), new Document(
                              Sensor.KEY_LIGHT, sensorData.getLight())
                              .append(Sensor.KEY_PROX, sensorData.getProx())
                              .append(Sensor.KEY_TEMP, sensorData.getTemp())
                      ))));
  </code></pre>
  
  <p>Currently this code overrides the timestamp that's already in the db. </p>