Retrieve All Documents from a MongoDB Collection using Java

Maruthi Krishna
Updated on 10-Apr-2020 08:20:50

20K+ Views

You can retrieve documents from an existing collection in MongoDB using the find() method.Syntaxdb.coll.find()Where, db is the database.coll is the collection (name) in which you want to insert the documentExampleAssume we have a collection named students in the MongoDB database with the following documents −{name:"Ram", age:26, city:"Mumbai"} {name:"Roja", age:28, city:"Hyderabad"} {name:"Ramani", age:35, city:"Delhi"}The following query retrieves all the documents from the collected sample.> use myDatabase() switched to db myDatabase() > db.createCollection(sample) { "ok" : 1 } > > db.sample.find() { "_id" : ObjectId("5e870492af638d501865015f"), "name" : "Ram", "age" : 26, "city" : "Mumbai" } { "_id" : ObjectId("5e870492af638d5018650160"), "name" : "Roja", ... Read More

Insert Multiple Documents into a MongoDB Collection Using Java

Maruthi Krishna
Updated on 10-Apr-2020 08:18:40

2K+ Views

You can insert multiple documents into an existing collection in MongoDB using the insertMany() method.Syntaxdb.coll.insert(docArray)Where, db is the database.coll is the collection (name) in which you want to insert the documentdocArray is the array of documents you want to insert.Example> use myDatabase() switched to db myDatabase() > db.createCollection(sample) { "ok" : 1 } > db.test.insert([{name:"Ram", age:26, city:"Mumbai"}, {name:"Roja", age:28, city:"Hyderabad"}, {name:"Ramani", age:35, city:"Delhi"}]) BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Using ... Read More

Insert Document into MongoDB Collection Using Java

Maruthi Krishna
Updated on 10-Apr-2020 08:15:34

6K+ Views

You can insert a document into an existing collection in MongoDB using the insert() method.Syntaxdb.coll.insert(doc)Where, db is the database.coll is the collection (name) in which you want to insert the documentdoc is the document you want to insert.Example> use myDatabase() switched to db myDatabase() > db.createCollection(sample) { "ok" : 1 } > db.sample.insert({name:"Ram", age:26, city:"Hyderabad"}) WriteResult({ "nInserted" : 1 })Using Java programIn Java, you can insert a document into a collection using the insertOne() method of the com.mongodb.client.MongoCollection interface. This method accepts a document (object) representing the document you want to insert as a parameter.Therefore to create a collection in MongoDB ... Read More

Create MongoDB Collection Using Java

Maruthi Krishna
Updated on 10-Apr-2020 08:12:03

3K+ Views

You can create a collection in MongoDB using the db.createCollection() method.Syntaxdb.createCollection(name, options)Wheredb is the database.name is the name of the collection you want to create.Option is the set of optional parameters such as capped, auto indexed, size and, max.Example> use myDatabase switched to db myDatabase > db.createCollection("myCollection") { "ok" : 1 }Using Java programIn Java, you can create a collection using the createCollection() method of the com.mongodb.client.MongoDatabase interface. This method accepts a string value representing the name of the collection.Therefore to create a collection in MongoDB using Java program −Make sure you have installed MongoDB in your systemAdd the following dependency ... Read More

Draw a Filled Ellipse in OpenCV using Java

Maruthi Krishna
Updated on 10-Apr-2020 08:01:15

252 Views

The org.opencv.imgproc package of Java OpenCV library contains a class named Imgproc this class provides various methods to process an input image. It provides a set of methods to draw geometrical shapes on images.This class provides a method named ellipse() using this you can draw an ellipse on an image, one of the variants of this method allows you to specify the line type as one of the parameters including −A Mat object representing the image on which the ellipse is to be drawn.A RotatedRect object (The ellipse is drawn inscribed in this rectangle.)A Scalar object representing the color of the ... Read More

Draw Polylines in OpenCV Using Java

Maruthi Krishna
Updated on 10-Apr-2020 07:58:14

583 Views

The org.opencv.imgproc package of Java OpenCV library contains a class named Imgproc this class provides various methods to process an input image such as resize(), filter2D, etc.. In addition to these It also provides a set of method to draw geometrical shapes on images.Among them to draw a polylines you need to invoke the polylines() method of this class. This method accepts the following parameters −A Mat object representing the image on which the polygon is to be drawn.A-List object holding the objects of the type MatOfPoint.A parameter of the type boolean specifying whether the poly-lines are closed.A Scalar object ... Read More

Draw a Polygon in OpenCV Using Java

Maruthi Krishna
Updated on 10-Apr-2020 07:55:56

534 Views

A polygon with all the interior angles less than 180 is known as a convex polygon. The org.opencv.imgproc package of Java OpenCV library contains a class named Imgproc. To draw a polygon you need to invoke the fillConvexPoly() method of this class. This method accepts 3 parameters −A Mat object representing the image on which the polygon is to be drawn.A MatOfPoint object points between which the polygon is to be drawn.A Scalar object representing the color of the polygon.Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawingConvexPolygon {    public ... Read More

Draw Arrowed Line in OpenCV Using Java

Maruthi Krishna
Updated on 10-Apr-2020 07:53:59

225 Views

The org.opencv.imgproc package of Java OpenCV library contains a class named Imgproc this class provides various methods to process an input image. It provides a set of methods to draw geometrical shapes on images.To draw an arrowed line you need to invoke the arrowedLine() method of this class. This method accepts the following parameters −A Mat object representing the image on which the line is to be drawn.Two Point objects representing the points between which the line is to be drawn.A Scalar object representing the color of the line. (BGR)An integer representing the thickness of the line(default:1).Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; ... Read More

Draw a Filled Circle in OpenCV Using Java

Maruthi Krishna
Updated on 10-Apr-2020 07:48:35

667 Views

The org.opencv.imgproc package of Java OpenCV library contains a class named Imgproc. This class provides a method named circle(), using this you can draw a circle on an image. This method provides the following parameters −A Mat object representing the image on which the circle is to be drawn.A Point object representing the center of the circle.An integer variable representing the radius of the circle.A Scalar object representing the color of the circle(BGR).An integer representing the thickness of the circle(default 1).If you pass Imgproc.FILLEDas line type, this method generates/draws a filled circle.Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Scalar; import ... Read More

Convert OpenCV Mat Object to JavaFX WritableImage

Maruthi Krishna
Updated on 10-Apr-2020 07:46:37

380 Views

If you try to read an image using the OpenCV imread() method it returns a Mat object. If you want to display the contents of the resultant Mat object using a JavaFX window You need to convert the Mat object to an object of the class javafx.scene.image.WritableImage. To do so, you need to follow the steps given below −Encode the Mat to MatOfByte − First of all, you need to convert the matrix to the matrix of a byte. You can do it using the method imencode() of the class Imgcodecs.This method accepts a String parameter(specifying the image format), a ... Read More

Advertisements