Java 向 MongoDB 中的数组插入值

标签 java arrays json mongodb push

我是 MongoDB 的新手,完全被查询弄糊涂了。我只需要通过将字符串值(例如:Temperature)添加到字符串列表来更新 mongodb 数据库中的文档。从研究中我知道我必须为此使用 $push 方法。我认为代码必须看起来像这样:

BasicDBObject newDocument = new BasicDBObject().append("$set", 
new BasicDBObject().append("Subscribed Topics", topic));

collection.update(new BasicDBObject().append("Sensor Type", sensorType), newDocument);
new BasicDBObject("$push",
new BasicDBObject("Subscribed Topics", topic));

带有数组的字段称为“订阅的主题”,“主题”是一个字符串(温度)。然后我想用相应的“传感器类型”更新集合中的文档。但是,我真的不知道如何正确调用 $push 部分。希望有人能帮我整理一下这部分代码。

最好的问候。

更新,我尝试按照重复问题中的建议实现,但仍然出现错误。非常不确定这是否是正确的方法。

 DBObject listItem = new BasicDBObject("Subscribed Topics", "Light");
             DBObject updateQuery = new BasicDBObject("$push", listItem);
             collection.update(query, updateQuery);`

我为关键订阅主题(数组)创建了一个值为 Light in 的新对象。为什么我要把它推送到一个新的对象?

最佳答案

我的天哪!这个问题让我再次进入了被遗忘已久的 Java 世界——经过这么多年…… ;) Anyhoo,这里有一个完整的工作示例,可能会让您了解正在发生的事情。您可以多次运行代码,看看“已订阅主题”数组中的元素数量是如何增加的。

我使用了以下驱动程序:https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/3.3.0/mongo-java-driver-3.3.0.jar

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;

import org.bson.Document;
import org.bson.conversions.Bson;

import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;

public class MongoDbPush {
  public static void main(String[] args)
  {
    MongoClient mongoClient = new MongoClient();
    MongoDatabase database = mongoClient.getDatabase("pushExampleDb");
    MongoCollection<Document> collection = database.getCollection("pushExampleCollection");

    String sensorType = "Temperature";

    // try to load existing document from MongoDB
    Document document = collection.find(eq("Sensor Type", sensorType)).first();
    if(document == null)
    {
      // no test document, let's create one!
      document = new Document("Sensor Type", sensorType);

      // insert it into MongoDB
      collection.insertOne(document);

      // read it back from MongoDB
      document = collection.find(eq("Sensor Type", sensorType)).first();
    }

    // see what it looks like in JSON (on the first run you will notice that it has got an "_id" but no "Subscribed Topics" array yet)
    System.out.println(document.toJson());

    // update the document by adding an entry to the "Subscribed Topics" array
    Bson filter = eq("Sensor Type", sensorType);
    Bson change = push("Subscribed Topics", "Some Topic");
    collection.updateOne(filter, change);

    // read one more time from MongoDB
    document = collection.find(eq("Sensor Type", sensorType)).first();

    // see what the document looks like in JSON (on the first run you will notice that the "Subscribed Topics" array has been created and has got one element in it)
    System.out.println(document.toJson());

    mongoClient.close();
  }
}

关于Java 向 MongoDB 中的数组插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45333966/

相关文章:

java - 填充数组并调用 super

javascript - 为什么我必须先将此 Javascript 对象存储在变量中,然后再将其推送到数组?

php - 将 JSON 字典值存储到变量中

java - 在 VSCode 中使用 Kotlin

java - android opencv 3.4.1 构建命令失败。 undefined reference

arrays - RxJS 限制和返回响应结果数组的最佳方式

python - 如何在 3d numpy 数组中查找 2d 数组的行

java - 如何从 json 响应中获取子对象

javascript - 如何在浏览器中显示纯文本,从 API 获取

java - Android 时间小部件未接收更新