Java 向 MongoDB 中的数组插入值
Java insert value to Array in MongoDB
我是 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));
带有数组的字段称为"Subscribed Topics","topic"是一个字符串(温度)。然后我想用对应的"Sensor Type"更新集合中的文档。但是,我真的不知道如何正确调用 $push 部分。希望有人能帮我整理一下这部分代码
此致。
更新,我尝试按照重复问题中的建议实施,但仍然出现错误。非常不确定这是否是正确的方法。
DBObject listItem = new BasicDBObject("Subscribed Topics", "Light");
DBObject updateQuery = new BasicDBObject("$push", listItem);
collection.update(query, updateQuery);`
我为关键订阅主题(数组)创建了一个值为 Light in 的新对象。那我为什么要把它推送到一个新的对象?
我的天哪!这个问题让我再次进入了早已被遗忘的 Java 世界 - 经过这么多年...... ;) Anyhoo,这是一个完整的工作示例,可能会让您了解正在发生的事情。您可以多次 运行 代码,并查看 "Subscribed Topics" 数组中的元素数量如何增加。
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();
}
}
上述方法仍然有效,但是,使用更新的 Mongo 驱动程序,下面也是一个可行的机制。
以下适用于 Mongo Driver 3.6 及更高版本(在本例中使用 3.12.4)
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("pushExampleDb");
MongoCollection<Document> collection = database.getCollection("pushExampleCollection");
collection.findOneAndUpdate(Filters.eq("Sensor Type",<theSensorTypeNameComesHere>),
Updates.pushEach("Subscribed Topics",<listContainingTheValuesComeHere>));
我是 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));
带有数组的字段称为"Subscribed Topics","topic"是一个字符串(温度)。然后我想用对应的"Sensor Type"更新集合中的文档。但是,我真的不知道如何正确调用 $push 部分。希望有人能帮我整理一下这部分代码
此致。
更新,我尝试按照重复问题中的建议实施,但仍然出现错误。非常不确定这是否是正确的方法。
DBObject listItem = new BasicDBObject("Subscribed Topics", "Light");
DBObject updateQuery = new BasicDBObject("$push", listItem);
collection.update(query, updateQuery);`
我为关键订阅主题(数组)创建了一个值为 Light in 的新对象。那我为什么要把它推送到一个新的对象?
我的天哪!这个问题让我再次进入了早已被遗忘的 Java 世界 - 经过这么多年...... ;) Anyhoo,这是一个完整的工作示例,可能会让您了解正在发生的事情。您可以多次 运行 代码,并查看 "Subscribed Topics" 数组中的元素数量如何增加。
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();
}
}
上述方法仍然有效,但是,使用更新的 Mongo 驱动程序,下面也是一个可行的机制。
以下适用于 Mongo Driver 3.6 及更高版本(在本例中使用 3.12.4)
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("pushExampleDb");
MongoCollection<Document> collection = database.getCollection("pushExampleCollection");
collection.findOneAndUpdate(Filters.eq("Sensor Type",<theSensorTypeNameComesHere>),
Updates.pushEach("Subscribed Topics",<listContainingTheValuesComeHere>));