MongoDB Java PDF
MongoDB Java PDF
MongoDB - Java
Advertisements
Installation
Before you start using MongoDB in your Java programs, you need to make sure that you
have MongoDB JDBC driver and Java set up on the machine. You can check Java tutorial
for Java installation on your machine. Now, let us check how to set up MongoDB JDBC
driver.
You need to download the jar from the path Download mongo.jar . Make sure to
download the latest release of it.
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;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
Now, let's compile and run the above program to create our database myDb as shown
below.
$javac ConnectToDB.java
$java ConnectToDB
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;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
//Creating a collection
database.createCollection("sampleCollection");
System.out.println("Collection created successfully");
}
}
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 2/10
2/28/2018 MongoDB Java
Getting/Selecting a Collection
To get/select a collection from the database, getCollection() method of
com.mongodb.client.MongoDatabase class is used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Creating a collection
System.out.println("Collection created successfully");
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("myCollection");
System.out.println("Collection myCollection selected successfully");
}
}
Insert a Document
To insert a document into MongoDB, insert() method of
com.mongodb.client.MongoCollection class is used.
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 3/10
2/28/2018 MongoDB Java
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 4/10
2/28/2018 MongoDB Java
import com.mongodb.client.MongoDatabase;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
Document{{
_id = 5967745223993a32646baab8,
title = MongoDB,
id = 1,
description = database,
likes = 100,
url = http://www.tutorialspoint.com/mongodb/, by = tutorials point
}}
Document{{
_id = 7452239959673a32646baab8,
title = RethinkDB,
id = 2,
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 5/10
2/28/2018 MongoDB Java
description = database,
likes = 200,
url = http://www.tutorialspoint.com/rethinkdb/, by = tutorials point
}}
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;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection myCollection selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 6/10
2/28/2018 MongoDB Java
}
}
}
Delete a Document
To delete a document from the collection, you need to use the deleteOne() method of the
com.mongodb.client.MongoCollection class.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
while (it.hasNext()) {
System.out.println("Inserted Document: "+i);
System.out.println(it.next());
i++;
}
}
}
Dropping a Collection
To drop a collection from a database, you need to use the drop() method of the
com.mongodb.client.MongoCollection class.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Creating a collection
System.out.println("Collections created successfully");
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 8/10
2/28/2018 MongoDB Java
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
// Dropping a Collection
collection.drop();
System.out.println("Collection dropped successfully");
}
}
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 9/10
2/28/2018 MongoDB Java
myCollection1
myCollection5
Remaining MongoDB methods save(), limit(), skip(), sort() etc. work same as
explained in the subsequent tutorial.
Advertisements
YouTube 52K
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 10/10