SELECT vs FIND() [search records in Mongo way]


In the previous post we have seen some of the basic operations we perform on the mongo db from java. Today we will focus only on the SELECT operations.

In this post we will go through different searching operations such as , Greater than, less than, equals,  and some or the Logical operations such as And, Or, Not  and others such as  Starts with, Ends With, Contains,IN etc .

Before going into the program, lets setup a basic database and sample collections data.
Run the below script on your mongoDB, so that you will be ready with test data.

use mydb{"name":"jhon","email":"","password":"jhon134","salary":2000}){"name":"casey","email":"","password":"casey","salary":1025}){"name":"candis","email":"","password":"candis","salary":100}){"name":"marcus","email":"","password":"marcus","salary":250125})

The key point here is  to perform equal, less than, greater than, and Logical operations, the mongo java driver 3.2 provided very use full class com.mongodb.client.model.Filters.
It is a final class provides many static methods, to perform the search operations.

To perform string search operations such as Starts with, ends with, contains we need to opt for regular expressions.The important point here is , in relations databases we use ‘%’ the equivalent value for that in Mongo db is ‘.*’

Having the database ready, now lets jump into the code, all the respective operations description is written in comments.

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import static com.mongodb.client.model.Filters.regex;
import java.util.ArrayList;
import java.util.regex.Pattern;
import org.bson.Document;

public class SomeMoreSelectOperations {

    public static void main(String[] args) {

        // connect to mongodb
        MongoClient mongoClient = new MongoClient("localhost");
        MongoDatabase database = mongoClient.getDatabase("mydb");

        MongoCollection collection = database.getCollection("user");

        // equals
        collection.find(Filters.eq("name", "jhon"));

        // less than
        collection.find("salary", 200));

        // greater than
        collection.find("salary", 200));

        //Logical operations

        // where salary less than 200 and greater than 100
        collection.find(Filters.and(("salary", 200)), ("salary", 1000))));

        // where salary less than 200 or greater than 100
        collection.find(Filters.or(("salary", 200)), ("salary", 1000))));

        // where salary neither 200 nor 1000
        collection.find(Filters.nor(("salary", 200)), ("salary", 1000))));

        // not equals
        collection.find(Filters.not(Filters.eq("salary", 200)));

        //IN operation
        ArrayList values = new ArrayList();
        collection.find("salary", values));

        // contains
        Pattern pattern = Pattern.compile(".*" + Pattern.quote("contain string") + ".*", Pattern.CASE_INSENSITIVE);
        MongoCursor<Document> cursor = collection.find(regex("columnName", pattern)).iterator();

       // For startswith
        Pattern pattern1 = Pattern.compile("^" + Pattern.quote("begin string"), Pattern.CASE_INSENSITIVE);
        MongoCursor<Document> cursor1 = collection.find(regex("columnName", pattern1)).iterator();

       // For endswith
        Pattern pattern2 = Pattern.compile(Pattern.quote("end string") + "$", Pattern.CASE_INSENSITIVE);
        MongoCursor<Document> cursor2 = collection.find(regex("columnName", pattern)).iterator();

        // iterate throght the cursor
        while (cursor2.hasNext()) {
            Document doc =;




About sivateja

I am a professional graduate..I am passionate about Latest Technology.
This entry was posted in mongodb and tagged , , . Bookmark the permalink.

One Response to SELECT vs FIND() [search records in Mongo way]

  1. Pingback: SivaPulse

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s