Javaを使用してMongoDBコレクションの既存のドキュメントを更新するにはどうすればよいですか?
update() メソッドは、既存のドキュメントの値を更新します。
構文
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Javaでは、 updateOne()を使用して単一のドキュメントを更新できます。 com.mongodb.client.MongoCollectionのメソッド インターフェース。このメソッドには、更新用のフィルターと値を渡す必要があります。
例
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
public class UpdatingDocuments {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
//Connecting to the database
MongoDatabase database = mongo.getDatabase("myDatabase");
//Creating a collection object
MongoCollection<Document>collection = database.getCollection("myCollection");
//Preparing documents
Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Hyderabad");
Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam");
Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi");
//Inserting the created documents
List<Document> list = new ArrayList<Document>();
list.add(document1);
list.add(document2);
list.add(document3);
collection.insertMany(list);
//Updating a document
collection.updateOne(Filters.eq("name", "Robert"), Updates.set("city", "Delhi"));
System.out.println("Document update successfully...");
FindIterable<Document> iterDoc = collection.find();
Iterator it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
} 出力
Document update successfully...
Document after update:
Document{{_id=5e86dd21e9b25f3460b1abe0, name=Ram, age=26, city=Hyderabad}}
Document{{_id=5e86dd21e9b25f3460b1abe1, name=Robert, age=27, city=Vishakhapatnam}}
Document{{_id=5e86dd21e9b25f3460b1abe2, name=Rhim, age=30, city=Delhi}} -
Java 9でJavaFXを使用してJShellを実装するにはどうすればよいですか?
JShell サンプル式を実装するために使用されるインタラクティブなツールです。 JavaFXを使用してプログラムでJShellを実装できます アプリケーションの場合、以下にリストされているJavaプログラムにいくつかのパッケージをインポートする必要があります import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet; 以下の例では、サンプルのJavaFXアプリケーションを実装しました。 テキストフィールドにさまざまな値を入力します 「評価」を押します 「」ボタン。対応する
-
Javaを使用してOpenCVでポリラインを描画する方法は?
Java OpenCVライブラリのorg.opencv.imgprocパッケージには、 Imgprocという名前のクラスが含まれています。 このクラスは、resize()、filter2Dなどの入力画像を処理するためのさまざまなメソッドを提供します。これらに加えて、画像に幾何学的形状を描画するための一連のメソッドも提供します。 それらの中でポリラインを描画するには、 polylines()を呼び出す必要があります このクラスのメソッド。このメソッドは、次のパラメーターを受け入れます- ポリゴンが描画される画像を表すマットオブジェクト。 タイプMatOfPointのオブジェクトを保