我正在尝试在另一个文档中添加一个文档。
我正在尝试将带有时间戳的新文档作为键和光代理和临时作为该文档的内容插入到 documentsensor_collection
中。
我的代码不起作用是合乎逻辑的,因为我正在设置一个新的sensor_collection
. 有谁知道我如何设置时间戳文档,sensor_collection
或者建议不要这样做?
这是代码:
MongoCollection<Document> 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())
))));
目前,此代码会覆盖数据库中已经存在的时间戳。