使用Java 3驱动程序更新MongoDB [英] MongoDB update using Java 3 driver

查看:104
本文介绍了使用Java 3驱动程序更新MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要切换到MongoDB Java驱动程序版本3.我不知道如何执行文档更新.例如,我要更改用户的年龄":

I'm switching to the MongoDB Java driver version 3. I cannot figure out how to perform an update of a Document. For example, I want to change the "age" of an user:

MongoDatabase db = mongoClient.getDatabase("exampledb");
MongoCollection<org.bson.Document> coll = db.getCollection("collusers");

Document doc1 = new Document("name", "frank").append("age", 55) .append("phone", "123-456-789");
Document doc2 = new Document("name", "frank").append("age", 33) .append("phone", "123-456-789");
coll.updateOne(doc1, doc2); 

输出为:

java.lang.IllegalArgumentException: Invalid BSON field name name

任何想法如何解决? 谢谢!

Any idea how to fix it ? Thanks!

推荐答案

使用:

coll.updateOne(eq("name", "frank"), new Document("$set", new Document("age", 33)));

用于更新找到的第一个文档.要进行多次更新:

for updating the first Document found. For multiple updates:

coll.updateMany(eq("name", "frank"), new Document("$set", new Document("age", 33)));

在此链接上,您可以罚款快速对MongoDB Java 3驱动程序的引用

On this link, you can fine a quick reference to MongoDB Java 3 Driver

这篇关于使用Java 3驱动程序更新MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆