Javaを使用してMongoDBコレクション内の複数のドキュメントを更新するにはどうすればよいですか?
updateMany()の使用 コレクションのすべてのドキュメントを更新できる方法。
db.COLLECTION_NAME.update(<filter>, <update>)
Javaでは、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 org.bson.conversions.Bson;
import com.mongodb.MongoClient;
public class UpdatingMultipleDocuments {
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", "Delhi");
Document document3 = new Document("name", "Rahim").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);
System.out.println("List of the documents: ");
FindIterable<Document> iterDoc = collection.find();
Iterator it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//Updating multiple documents
Bson filter = new Document("city", "Delhi");
Bson newValue = new Document("city", "Vijayawada");
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateMany(filter, updateOperationDocument);
System.out.println("Document update successfully...");
System.out.println("List of the documents after update");
iterDoc = collection.find();
it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
} 出力
List of the documents:
Document{{_id=5e88a61fe7a0124a4fc51b2c, name=Ram, age=26, city=Hyderabad}}
Document{{_id=5e88a61fe7a0124a4fc51b2d, name=Robert, age=27, city=Delhi}}
Document{{_id=5e88a61fe7a0124a4fc51b2e, name=Rahim, age=30, city=Delhi}}
Document update successfully...
List of the documents after update
Document{{_id=5e88a61fe7a0124a4fc51b2c, name=Ram, age=26, city=Hyderabad}}
Document{{_id=5e88a61fe7a0124a4fc51b2d, name=Robert, age=27, city=Vijayawada}}
Document{{_id=5e88a61fe7a0124a4fc51b2e, name=Rahim, age=30, city=Vijayawada}} -
Javaを使用してMongoDBコレクションからデータを取得しながら、レコード数を制限するにはどうすればよいですか?
MongoDBコレクションからレコードを取得するときに、を使用して結果のレコード数を制限できます。 limit() メソッド。 構文 db.COLLECTION_NAME.find().limit(no.of records needed) Java MongoDBライブラリは同じ名前のメソッドを提供し、レコード数を制限して、必要なレコード数を表す整数値をバイパスしてこのメソッドを呼び出します(find()メソッドの結果)。 例 import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollect
-
Javaを使用してMongoDBからデータを取得しているときにドキュメントをスキップするにはどうすればよいですか?
MongoDBコレクションからレコードを取得しているときに、 skip()を使用して結果のレコードをスキップできます。 メソッド。 構文 db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) Java MongoDBライブラリは、同じ名前のメソッドを提供します。レコードをスキップするには、スキップするレコードの数を表す整数値をバイパスして、このメソッドを(find()メソッドの結果で)呼び出します。 例 import com.mongodb.client.FindIterable; import com.mongodb.client.Mon