Java 在MongoDB中将一个文档插入到另一个文档中

Java 在MongoDB中将一个文档插入到另一个文档中,java,mongodb,mongodb-java,mongo-collection,Java,Mongodb,Mongodb Java,Mongo Collection,我正在尝试在另一个文档中添加文档 我正在尝试将一个新文档插入文档传感器\u集合,其中时间戳作为密钥,light prox和temp作为该文档的内容 我的代码不起作用是合乎逻辑的,因为我正在设置一个新的sensor\u集合。是否有人知道如何在sensor\u collection中设置时间戳文档,或者建议不要这样做 代码如下: MongoCollection<Document> collection = db.getCollection(Sensor.KEY_COLLECTION

我正在尝试在另一个文档中添加文档

我正在尝试将一个新文档插入文档
传感器\u集合
,其中时间戳作为密钥,light prox和temp作为该文档的内容

我的代码不起作用是合乎逻辑的,因为我正在设置一个新的
sensor\u集合
。是否有人知道如何在
sensor\u 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())
                    ))));
MongoCollection collection=db.getCollection(Sensor.KEY\u collection);
//将传感器数据附加到现有文档中
collection.updateOne(文档,新文档($set),
新文档(“传感器集合”,新文档(
String.valueOf(stamp.getCurrentTime()),新文档(
Sensor.KEY_灯,sensorData.getLight())
.append(Sensor.KEY\u PROX,sensorData.getProx())
.append(Sensor.KEY\u TEMP,sensorData.getTemp())
))));

当前,此代码覆盖数据库中已经存在的时间戳

如果要附加到现有的嵌入式集合,请使用
$push
而不是
$set
$push
运算符将指定的值附加到数组中。大概是这样的:

collection.updateOne(doc, new Document("$push",
            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())
                    ))));
有关mongo更新操作符的更多详细信息,请参见我在中找到的:

要在嵌入文档或数组中指定
,请使用点表示法

我使用了$set操作符。我正在设置sensor_collection.timestamp

MongoCollection<Document> collection  =  db.getCollection(Sensor.KEY_COLLECTION);
    //append sensor data to existing document
    collection.updateOne(doc, new Document("$set",
            new Document("sensor_collection."+String.valueOf(stamp.getCurrentTime()),
                     new Document(
                            Sensor.KEY_LIGHT, sensorData.getLight())
                            .append(Sensor.KEY_PROX, sensorData.getProx())
                            .append(Sensor.KEY_TEMP, sensorData.getTemp())
                    )));
MongoCollection collection=db.getCollection(Sensor.KEY\u collection);
//将传感器数据附加到现有文档中
collection.updateOne(文档,新文档($set),
新文档(“传感器集合”+String.valueOf(stamp.getCurrentTime()),
新文件(
Sensor.KEY_灯,sensorData.getLight())
.append(Sensor.KEY\u PROX,sensorData.getProx())
.append(Sensor.KEY\u TEMP,sensorData.getTemp())
)));
这很有效。给出:


我有点迷路了。是否要将新文档附加到现有集合?嘿,在sensor_集合中,我希望有多个时间戳,每个时间戳包含灯光、prox和temp。我想向sensor_collection添加另一个时间戳对象。嘿,谢谢你的回答$如果传感器收集是阵列,则推送工作。它是一个物体。是否仍可以将另一个时间戳对象添加到传感器集合?不使用数组的。或者使用数组是更常见的方法吗?您可以选择嵌入的集合而不是整个集合,然后只更新它。也许这是可行的,但我想使用数组会更简单