对MongoDB中的数组插入Java的值
问题描述:
im MongoDB中的新增内容,并且完全被查询困惑。我只需要通过在字符串列表中添加字符串值(例如:Temperature)来更新mongodb数据库中的文档。从研究中我知道我必须使用$ push方法。我觉得代码以某种方式是这样的:对MongoDB中的数组插入Java的值
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);`
我创建了一个新的Object,其值为Light In for Key订阅主题(数组)。为什么我把它推到一个新的对象呢?
答
我的天啊!这个问题让我再一次沉浸在久已遗忘的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();
}
}
+0
对于我的情况,它工作正常,非常感谢!不,我需要检查主题是否已经可用,但我会自己找到;)(addToSet) –
[(MongoDB的爪哇)$推入阵列(的可能的复制https://stackoverflow.com/questions/15436542/mongodb-java-push-into -array) – dnickless
我看到那篇文章。它几乎相同。事件我的数组结构更简单我没有得到如何适应它到我的用例。 –
下一个可能会帮助(请注意,他们在这里使用适当的业务对象,再加上他们只创建不改变的东西,所以忘了它 - 我很抱歉):https://stackoverflow.com/questions/15371839/how-to-add-an-array-to-a-mongodb-document-using-java(我想,你一直在查看StackOverflow已经很多了) – dnickless