MongoDB- Java
In this article, we’ll take a look at integrating MongoDB (a popular NoSQL database) with Java.
Prerequisites-
You need to make sure that you have MongoDB JDBC driver and Java set up on the machine.
Connect to Database
To connect database, you need to specify the database name, if the database doesn't exist then MongoDB creates it automatically.
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient;
public class Connect
{
public static void main( String args[] )
{
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
//Accessing the database
MongoDatabase database = mongo.getDatabase("myDb");
System.out.println("Connected !);
}
}
Create a Collection
To create a collection, createCollection()method of
com.mongodb.client.MongoDatabase class is used.
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
public class CreateCollection
{
public static void main( String args[] )
{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
MongoDatabase database =mongo.getDatabase("myDatabase"); database.createCollection("TestCollection");
System.out.println ("Collection created successfully");
}
}
Insert a Document
To insert a document into MongoDB, insert() method of com.mongodb.client.MongoCollection class is used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import org.bson.Document;
import com.mongodb.MongoClient;
public class InsertDocument
{
public static void main( String args[] )
{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongo.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("FirstCollection");
Document document = new Document("title", "FirstDocument")
.append("id", 1)
.append("description", "database")
.append("likes", 100);
collection.insertOne(document);
System.out.println("Document inserted ");
}
}
Retrieve All Documents
To select documents from the collection, find() method of
com.mongodb.client.MongoCollection class is used.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
public class MongoTest
{
public static void main( String args[] )
{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongo.getDatabase("myDb");
MongoCollection<Document> collection = database.getCollection("FirstCollection");
FindIterable<Document> iterDoc = collection.find();
int i = 1;
Iterator it = iterDoc.iterator();
while (it.hasNext())
{
System.out.println(it.next());
i++;
}
}
}
Delete Document
To Delete document we need to use deleteOne() method.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;importcom.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
public class DeleteDocument
{
public static void main( String args[] )
{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongo.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("FirstCollection");
collection.deleteOne(Filters.eq("id", 1));
System.out.println("Document deleted successfully...");
FindIterable<Document> iterDoc = collection.find();
int i = 1;
Iterator it = iterDoc.iterator();
while (it.hasNext())
{
System.out.println("Inserted Document: "+i);
System.out.println(it.next());
i++;
}
}
}
Update Document
To update a document from the collection, updateOne() method of com.mongodb.client.MongoCollection class is used.
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.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
public class UpdateDocument
{
public static void main( String args[] )
{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongo.getDatabase("myDatabase");
MongoCollection<Document> collection = database.getCollection("FirstCollection");
collection.updateOne(Filters.eq("id", 1), Updates.set("likes",150)); System.out.println("Document update successfully...");
FindIterable<Document> iterDoc = collection.find();
int i = 1;
Iterator it = iterDoc.iterator();
while (it.hasNext())
{
System.out.println(it.next());
i++;
}
}
}
}
Thats it for now !
Write me on Google+ deshpandeaditya111@gmail.com
Comments
Post a Comment