【发布时间】:2015-12-19 16:07:29
【问题描述】:
我正在尝试在另一个文档中添加一个文档。
我正在尝试将一个新文档插入到文档sensor_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())
))));
目前,此代码会覆盖数据库中已经存在的时间戳。
【问题讨论】:
-
我有点迷路了。要将新文档附加到现有集合吗?
-
嘿,在 sensor_collection 中我想要多个时间戳,每个时间戳都包含光照、代理和温度。我想向 sensor_collection 添加另一个时间戳对象。
标签: java mongodb mongodb-java mongo-collection