Query MongoDB using the Java Driver

In this blog I will demonstrate how to get start with MongoDB Java Driver with examples on how to execute some common queries to the database.

Set up

I use Spring Boot (v1.2.1) with Maven for my project setup and include the following Maven dependency


My MongoDB server is on version 2.6

Example Document

For the rest of this blog, I will use the following example document

    "firstname" : "John",
    "lastname" : "Smith",
    "fullname" : "John N Smith"
    "age" : 25,
    "qualifications" : [
        "Master of Science",
        "B. Eng"

Exact Match

The Java driver API implements a query as a DBObject which is then used as an input to the Collection’s find method for executing a query. The query DBObject can be constructed using a BasicDBObjectBuilder. The following codes demonstrate how to find all documents within a collection with field name “firstname” with value “John”.

 BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("firstname", "john");
 DBObject query = builder.get();
 DB db = mongoClient.getDB("mydb");
 DBCursor cursor = db.getCollection("myCollection").find(query);
 List<DBObject> dbObjects = cursor.skip(5).limit(100).toArray();

Note the 5 and 100 in the last line to indicate the first and the maximum number of records to return by the cursor.


To perform like query as in relational database, you have to use regular expression when building the query DBObject using BasicDBObjectBuilder. For example,

BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
                              .add("fullname", Pattern.compile("john"));

will return documents with fullname “john smith” and “lee john” etc.

Greater Than &/or Less Than

To query with greater than or less than, create a DBObject using $gt(e) and $lt(e).

To get documents with age greater than 18:

BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
                               .add("age", new BasicDBObject("$gt", 18));

To get documents with age less than 50:

BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
                              .add("age", new BasicDBObject("$lt", 50));

To get documents with age between 18 and 50 (exclusive):

BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
                            .add("age", new BasicDBObject("$lt", 50).append("$gt", 18));

Match Value in Array

To query for all documents with matching element in array. For example to find all persons with qualification “B. Eng.”

 BasicDBObject in = new BasicDBObject("$elemMatch", new BasicDBObject("$eq", "B. Eng."));
 BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("qualifications", in);

Combining query criteria

The BasicDBObjectBuilder can be used to combine multiple query criteria together, for example

 BasicDBObjectBuilder builder = BasicDBObjectBuilder.start()
                                .add("firstname", "john")
                                .add("age", new BasicObject("$gt", 18))

About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.