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

mongoselect.png

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

db.user.save({"name":"jhon","email":"jhon@gmail.com","password":"jhon134","salary":2000})
db.user.save({"name":"casey","email":"casey@gmail.com","password":"casey","salary":1025})
db.user.save({"name":"candis","email":"candis@gmail.com","password":"candis","salary":100})
db.user.save({"name":"marcus","email":"marcus@gmail.com","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(Filters.lt("salary", 200));

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

        //Logical operations

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

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

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

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

        //IN operation
        ArrayList values = new ArrayList();
        values.add(200);
        values.add(100);
        values.add(3000);
        collection.find(Filters.in("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 = cursor2.next();
            System.out.println(doc);
        }

    }

References:
http://stackoverflow.com/questions/37120845/mongodb-find-contains-startswith-endswith-and-exact-string-with-java-driver

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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