Java MongoDB:ドキュメントの更新
このチュートリアルでは、Java MongoDB API `collection.update()`を使用してドキュメントを更新する方法を説明します。
テストデータ
以下のデータ/文書が挿入されていると仮定します。
{
"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. $ setを指定したDBCollection.update()
hosting = ‘hostB’のところで文書を検索し、クライアントの値を100から110に更新します。
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("clients", 110);
BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB");
collection.update(searchQuery, newDocument);
出力
{ "__id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "__id" : { "$oid" : "id"} , "clients" : 110}
{ "__id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
特定の値だけを更新するには、
$ set
update修飾子を使います。
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("clients", 110));
BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB");
collection.update(searchQuery, newDocument);
出力
{ "__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 ** + MongoDBチームは
DBCollection.replace()`という名前の別の新しいAPIを作成する必要があります。多くの初心者はこの `DBCollection.update()
APIに閉じ込められ、誤ってドキュメント全体を置き換えます。再び、特定の値を更新するには `$ set`を使います。
2. $ incを指定したDBCollection.update()
この例では、特定の値を増やすために `$ inc`修飾子を使用しています。 hosting = ‘hostB’の文書を検索し、値を100から199、(100 99)= 199に増やして ‘clients’値を更新します。
BasicDBObject newDocument =
new BasicDBObject().append("$inc",
new BasicDBObject().append("total clients", 99));
collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
出力
{ "__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()とmulti
この例は、一致したドキュメントのセットを更新するための `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);
出力
{ "__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"}
-
注意** `multi`を指定せずにupdateを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);
最初に一致した文書だけが更新されていることに気付きます。
{"__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}
一致するドキュメントのセットを更新するには、 “multi”をtrueに設定する必要があります。
4.完全な例
上記のコードスニペットを組み合わせて完全な例。
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();
}
}
}
出力
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で]。
http://docs.mongodb.org/manual/reference/operator/set/#op
.
S
set[$set
更新修飾子]。
http://docs.mongodb.org/manual/reference/operator/inc/#op
.
S
inc[$inc
MongoDB API、DBCollection JavaDoc]