原文出自:http://www.mkyong.com/mongodb/java-mongodb-update-document/
返回目录:http://ysj5125094.iteye.com/blog/2192754
Java MongoDB : Update document
In this tutorial, we show you how to use Java MongoDB API collection.update()
to update documents.
译:在本教程中,我们将向您展示如何使用MongoDB Java API的 collection.update() 更新文件。
Test Data
Assume following data / documents are inserted.
译:假设以下是插入的数据/文件。
{ "hosting" : "hostA", "type" : "vps", "clients" : 1000 }, { "hosting" : "hostB", "type" : "dedicated server", "clients" : 100 }, { "hosting" : "hostC", "type" : "vps", "clients" : 900 }
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
1. DBCollection.update() with $set
Find document where hosting = ‘hostB’, and update it’s clients values from 100 to 110.
译:查找 hosting = 'hostB' 的文档并且更新 client 字段的值(从100改到110)。
BasicDBObject newDocument = new BasicDBObject(); newDocument.put("clients", 110); BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB"); collection.update(searchQuery, newDocument);
Output...
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "clients" : 110} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
The document is replaced!? (译:文件被替换了?)
Wait, the entire “hostB” document is replaced with another new document, this is not what we want.
译: 等等,整个 'hostB' 文档被另一个新的文档所取代了,这不是我们想要的结果。
To update a particular value only, uses $set
update modifier.
译:更新一个特定的值,使用 $set 来处理。
BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject().append("clients", 110)); BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB"); collection.update(searchQuery, newDocument);
Output
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 110} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
Note
The MongoDB team should create another new API named DBCollection.replace()
, many beginners are trapped in this DBCollection.update()
API and replace the entire document accidentally. Again, to update a particular value, use $set
.
译:整个MongoDB团队应该创建一个新的API 取名 DBCollection.replace(),因为很多新手都受困于这个 DBCollection.update() API 和替换整个文档的意外之间。再次声明,更新一个特定的值要使用 $set。
2. DBCollection.update() with $inc
This example show the use of $inc
modifier to increase a particular value. Find document where hosting = ‘hostB’, update it’s ‘clients’ value by increasing the value from 100 to 199, (100 + 99) = 199.
译:这个例子显示,使用 $inc 修改增加一个特定的值。查找到hosting = 'hostB'的文档用新增加特定的值 来修改 'clients' 字段的值,由100改成199(100+99=199)。
BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99)); /* BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("total clients", 99)); */ collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
Output
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
3. DBCollection.update() with multi
This example show the use of multi
parameter to update a set of matched documents. Find document where type = ‘vps’, update all the matched documents’ ‘clients’ value to 888.
译:这个例子显示,使用 multi
参数更新设置匹配的文档。查找type='vps'的文档,更新所有匹配文档的'clients'字段为888.
BasicDBObject updateQuery = new BasicDBObject(); updateQuery.append("$set", new BasicDBObject().append("clients", "888")); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.append("type", "vps"); collection.updateMulti(searchQuery, updateQuery); //below statement set multi to true. //collection.update(searchQuery, updateQuery, false, true);
Output
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "clients" : "888" , "type" : "vps"}
Note
If update without the multi
set to true.
译:如果update的multi
没有设置为true。
BasicDBObject updateQuery = new BasicDBObject(); updateQuery.append("$set", new BasicDBObject().append("clients", "888")); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.append("type", "vps"); collection.update(searchQuery, updateQuery);
You will noticed that only the first matched document is updated.
译:你会注意到,只有第一个匹配的文档更新。
{"_id":{ "$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"} {"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} {"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
To update a set of matched documents, you need to set “multi
” to true.
译:更新一组匹配的文件,你需要设置“multi”为true。
4. Full Example
Full example by combining above code snippets.
译:结合上面的代码片段的完整示例。
package com.mkyong.core; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * Java MongoDB update document * * @author mkyong * */ public class UpdateApp { public static void printAllDocuments(DBCollection collection) { DBCursor cursor = collection.find(); while (cursor.hasNext()) { System.out.println(cursor.next()); } } public static void removeAllDocuments(DBCollection collection) { collection.remove(new BasicDBObject()); } public static void insertDummyDocuments(DBCollection collection) { BasicDBObject document = new BasicDBObject(); document.put("hosting", "hostA"); document.put("type", "vps"); document.put("clients", 1000); BasicDBObject document2 = new BasicDBObject(); document2.put("hosting", "hostB"); document2.put("type", "dedicated server"); document2.put("clients", 100); BasicDBObject document3 = new BasicDBObject(); document3.put("hosting", "hostC"); document3.put("type", "vps"); document3.put("clients", 900); collection.insert(document); collection.insert(document2); collection.insert(document3); } public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); // get a single collection DBCollection collection = db.getCollection("dummyColl"); System.out.println("Testing 1...no $set"); insertDummyDocuments(collection); // find hosting = hostB, and update the clients to 110 BasicDBObject newDocument = new BasicDBObject(); newDocument.put("clients", 110); BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB"); collection.update(searchQuery, newDocument); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("\nTesting 1...with $set"); insertDummyDocuments(collection); BasicDBObject updateDocument = new BasicDBObject(); updateDocument.append("$set", new BasicDBObject().append("clients", 110)); BasicDBObject searchQuery2 = new BasicDBObject().append("hosting", "hostB"); collection.update(searchQuery2, updateDocument); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("\nTesting 2... with $inc"); insertDummyDocuments(collection); // find hosting = hostB and increase it's "clients" value by 99 BasicDBObject newDocument2 = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99)); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument2); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("\nTesting 3... with $multi"); insertDummyDocuments(collection); // find type = vps , update all matched documents , clients value to 888 BasicDBObject updateQuery = new BasicDBObject(); updateQuery.append("$set", new BasicDBObject().append("clients", "888")); BasicDBObject searchQuery3 = new BasicDBObject(); searchQuery3.append("type", "vps"); collection.updateMulti(searchQuery3, updateQuery); // collection.update(searchQuery3, updateQuery, false, true); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }
Output
Testing 1...no $set { "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "clients" : 110} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900} Testing 1...with $set { "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 110} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900} Testing 2... with $inc { "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900} Testing 3... with $multi { "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900} { "_id" : { "$oid" : "id"} , "clients" : "888" , "hosting" : "hostA" , "type" : "vps"} Done
References
- How to do updating in MongoDB
- $set update modifier
- $inc update modifier
- Java MongoDB APIs , DBCollection JavaDoc
相关推荐
NULL 博文链接:https://study121007.iteye.com/blog/2307592
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
《Windows上安装MongoDB:完整步骤详解.pdf》是一份适用于Windows操作系统的MongoDB安装教程,适用于初学者和有经验的开发人员。本教程包括完整的步骤详解,可帮助您快速掌握MongoDB的安装配置,包括环境变量设置,...
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
文档层使用MongoDB:registered:有线协议,允许通过现有的MongoDB:registered:客户端绑定使用MongoDB:registered:API。 所有持久数据都存储在FoundationDB键值存储中。 文档层实现了MongoDB:registered:API(v ...
将SQL表转换为MongoDB集合的Java工具 轻松将 MSSQL 表转换为 MongoDB。 从下载 SQL Server JDBC 驱动程序 将 SQLJDBC4.jar 文件放在同一文件夹中。 一次指定 TABLE NAME 和 NUMBER OF ROWS 进行导入。 java -...
使用java向mongodb中插入数据 一、Linux安装mongodb 二、MongoDB客户端工具(MongoVUE)访问mongodb 三、通过Java操作MongoDB
Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for ...
java mongodb orm 实现-基于mongodb-driver开发的ORM
描述 PM2模块可自动监视mongodb的生命体征: 查询,输入,更新,删除 连接数 已用存储空间 网络速度(输入和输出) 代表名称和状态 pm2-mongodb ... pm2 set pm2-mongodb:ip 42.42.42.42 (我的mongod
docker run -d --name mongodb -p 27017:27017 -v /data/mongodb:/var/lib/mongodb anapsix/mongodb 作为客户: docker run -it --rm anapsix/mongodb mongo --help 配置 您可以像通常那样通过命令行传递配置选项...
mongoDB:MongoDB备忘单
mongodb人偶模块 目录 概述 从OS存储库或从MongoDB社区/企业存储库在RHEL / Ubuntu / Debian上安装MongoDB。 模块说明 ...如果只想使用默认选项安装服务器,则可以运行include mongodb::server 。
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
羽毛mongodb 用于数据库适配器,使用用于。 $ npm install --save mongodb feathers-mongodb ... connect ( 'mongodb://localhost:27017/feathers' ) . then ( client => { app . use ( '/messages' ,
1.java连接mongo数据库查询统计信息导出为excel表格 2.所有连接参数都可以动态输入,参数包括:ip、端口、数据库名称、集合名称、查询参数(公众号,写在指定文件中),导出表格存放位置
MongoDB Java操作大全 源代码 实例
Stitch SDK弃用通知Stitch SDK功能已移至Realm SDK。 使用之一,可以将使用... 安卓将以下内容添加到您的应用程序模块的build.gradle中: implementation ' org.mongodb:stitch-android-sdk:4.7.0 ' 还将以下内容添加到
The updated edition of this authoritative and accessible guide shows you the many advantages of using document-oriented databases, including how this secure, high-performance system enables flexible ...