Java MongoDB : Update document
2015-09-29 07:36
555 查看
In this tutorial, we show you how to use Java MongoDB API
1.
Find document where
Output
The document is replaced!?
Wait, the entire “
To update a particular value only, uses
Output
Note
The MongoDB team should create another new API named
2.
This example show the use of
Output
3.
This example show the use of
Output
Note
If update without the multi set to true.
You will noticed that only the first matched document is updated.
To update a set of matched documents, you need to set “
Output
Done
collection.update()to update documents.
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.
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.
To update a particular value only, uses
$setupdate modifier.
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.
2. DBCollection.update()
with $inc
This example show the use of $incmodifier 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.
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 multiparameter to update a set of matched documents. Find document where
type = ‘vps’, update all the matched documents’ ‘
clients’ value to 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.
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.
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
相关文章推荐
- MongoDB hello world example
- mongodb mapreduce应该注意的两个地方
- MongoDB 3.0 用户创建
- Mongodb之JavaApi调用例子
- 简单的给mongodb添加用户和认证
- mongodb连接
- MongoDB安装命令(wiredTiger引擎)
- ubuntu 安装 Mongodb
- Spring Batch Example – XML File To MongoDB Database(五)
- MongoDB集群怎样去访问?
- 【MongoDB初识】-条件操作符
- Mongodb3.0 UserManager
- mongodb的简单使用二(结合springdata实现增删改查)
- MongoDB副本集故障测试和解决方案
- MongoDB YAML格式的配置文件
- 【mongoDB中级篇①】游标cursor
- mongodb的简单使用一(环境搭建)
- mongodb授权和权限
- JAVA--将MongoDB数据写出到CSV
- MongoDB---windows安装