MongoDB Java Driver – Quick reference

The MongoDB Java Driver is a library that provides a Java-based interface for interacting with MongoDB, a popular NoSQL document database. It allows Java developers to connect to MongoDB databases, perform CRUD (Create, Read, Update, Delete) operations, and execute advanced queries using the Java programming language.

The MongoDB Java Driver serves as a bridge between your Java application and the MongoDB server, enabling seamless integration and communication between the two. It abstracts away the complexities of working directly with MongoDB’s wire protocol, providing a higher-level API that simplifies the development process.

If you are new to MongoDB we recommend checking this article: Building Java Enterprise applications using MongoDB

Driver Configuration

Firstly, let’s see how to configure your application to use MongoDB Driver. Add the following dependency to your Maven project:


Connecting to MongoDB

How to connect to a local Database and get an handle to DB named “mydb”

MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("mydb");

How to get an handle to the Collection named “test”

MongoCollection<Document> collection = database.getCollection("test");

Document management

How to create a Document and insert it:

Document doc = new Document("name", "MongoDB")
			   .append("type", "database")
			   .append("count", 1)
			   .append("info", new Document("x", 203).append("y", 102));


How to convert a Document into JSON:

String json = myDoc.toJson();

How to find the first Document in a Collection

Document myDoc = collection.find().first();

How to insert multiple Documents in one shot:

List<Document> documents = new ArrayList<Document>();
for (int i = 0; i < 100; i++) {
	documents.add(new Document("i", i));

How to count the number of Documents in a Collection:

System.out.println("total # of documents " + collection.count());

How to iterate over a Collection

MongoCursor<Document> cursor = collection.find().iterator();
try {
	while (cursor.hasNext()) {
} finally {

How to find one Document whose key matches with a value:

Document myDoc = collection.find(eq("i", 71)).first();

How to find all Documents which are matching a constraint:

MongoCursor<Document> cursor = collection.find(gt("i", 50)).iterator();

How to find all Documents which are matching multiple constraints:

MongoCursor<Document> cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator();

How to apply a filter which executes a Callback method defined in a Block class:

Block<Document> printBlock = new Block<Document>() {
	public void apply(final Document document) {
collection.find(gt("i", 50)).forEach(printBlock);

How to use a Projection:

Document myDoc = collection.find().projection(excludeId()).first();

How to update a Document:

collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110)));

How to update multiple Documents:

UpdateResult updateResult = collection.updateMany(lt("i", 100),
		new Document("$inc", new Document("i", 100)));

How to delete a Document

collection.deleteOne(eq("i", 110));

How to delete multiple Documents:

DeleteResult deleteResult = collection.deleteMany(gte("i", 100));

How to perform a Bulk write:

List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(new InsertOneModel<Document>(new Document("_id", 4)));
writes.add(new InsertOneModel<Document>(new Document("_id", 5)));
writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4)));