- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2617 Articles for Java
806 Views
In Java the com.mongodb.client.MongoCollection interface provides a method deleteMany(). Using this method you can delete multiple documents from a collection at once, to this method you need to pass the filter specifying the deletion criteria.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; 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 DeletingMultipleDocuments { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); ... Read More
549 Views
MongoDB db.dropDatabase() command is used to drop a existing database.This will delete the current database. if you haven't selected any, the default (test) database will be deleted.Syntaxdb.dropDatabase()In Java to delete a database, first of all, get the object of the required database using the getDatabase() method and delete it by invokimg the drop() method on it.Exampleimport com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; public class DropingDatabase { public static void main( String args[] ) { //Creating a MongoDB client @SuppressWarnings("resource") MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Creating a ... Read More
463 Views
In MongoDB to drop an index, you need to use dropIndex() method.Syntaxdb.COLLECTION_NAME.dropIndex({KEY:1})In Java, you can drop an Index using the dropIndex() method, to this method you need to pass the type of the index (ascending or descending) and the field name on which you have created it.dropIndex(Indexes.ascending("name"));Exampleimport com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Indexes; import org.bson.Document; import com.mongodb.MongoClient; public class DroppingIndex { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Accessing the database MongoDatabase database = ... Read More
532 Views
While retrieving records from a MongoDB collection, you can limit the number of records in the result using thelimit() method.Syntaxdb.COLLECTION_NAME.find().limit(no.of records needed)The Java MongoDB library provides a method with the same name, to limit the number of records invoke this method (on the result of the find() method) by passing an integer value representing the required number of records.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class LimitingRecords { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = ... Read More
845 Views
In MongoDB, you can view the list of databases using the show dbs command> show dbs admin config local myDatabase sampleDatabase students test testDBIn Java, you can get the list of all the databases in MongoDb using the getDatabaseNames() method.Exampleimport com.mongodb.client.MongoIterable; import com.mongodb.MongoClient; public class ListOfDatabases { public static void main( String args[] ) { // Creating a Mongo client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Retrieving the list of collections MongoIterable list = mongo.listDatabaseNames(); for (String name : list) { System.out.println(name); } } }Outputadmin config local myDatabase sampleDatabase students test testDB
1K+ Views
While retrieving data from MongoDb collections you can select only necessary data using projections. In Java, you can project necessary data while reading the documents from a collection using the projection() method. Invoke this method on the result of find(), bypassing the names of the required filed names as −projection(Projections.include("name", "age"));ExampleFollowing Java examples read the documents from a collection, using projection we are displaying the values of name and age fields only.import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Projections; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class ProjectionExample { public static void main( String args[] ... Read More
215 Views
While retrieving records from a MongoDB collection, you can skip records in the result using the skip() method.Syntaxdb.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)The Java MongoDB library provides a method with the same name, to skip records, invoke this method (on the result of the find() method) bypassing an integer value representing the number of records to skip.Exampleimport com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class SkipRecords { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); ... Read More
413 Views
The class ImageIcon is an implementation of the Icon interface that paints Icons from Images. You can display images on a Swing window using this class, the constructor of this class accepts a BufferedImage object as a parameter.Therefore to display an OpenCV image that is stored in a Mat object using Swing window, you need to convert it into a BufferedImage object and pass it as a parameter to the ImageIcon method.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesUsingSwings { public static ... Read More
337 Views
The JavaFX library provides a class with name ImageView using this you can display an image. This class accepts an object of the WritableImage class.To display an image Stored in OpenCV Mat object you need to convert it into a WritableImage object and pass it the ImageView class.Exampleimport java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DisplayingImagesJavaFX extends Application { @Override public void start(Stage stage) throws IOException { WritableImage writableImage = ... Read More
690 Views
Java 9 has introduced a new format called "jmod" to encapsulate modules. The jmod files can be designed to handle more content types than jar files. It can also package local codes, configuration files, local commands, and other types of data. The "jmod" format hasn't support at runtime and can be based on zip format currently. The jmod format can be used at both compile and link time and can be found in JDK_HOME\jmods directory, where JDK_HOME is a directory. The files in jmod format have a ".jmod" extension.Java 9 comes with a new tool called jmod, and it is located in the ... Read More