tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 No SQL > Mongo DB > How to use Like Query

How to use Like Query 

MongoDB is a scalable, high-performance, open source NoSQL database. It offers Ad hoc queries, Indexing, Replication, Load balancing, File storage (GridFS), Aggregation, MapReduce and Server-side JavaScript execution. This requires the library mongo-2.8.0.jar to be present in the classpath. The following example shows how to use like Query in Mongo database.

File Name  :  students.json

File Name  :  
com/bethecoder/tutorials/mongodb/tests/LikeQueryTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.mongodb.tests;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class LikeQueryTest {
  
  public static void main(String[] args) {
    try {
      //Connect to mongoDB with given IP and PORT
      Mongo mongo = new Mongo("localhost"27017);

      //Get the database object
      DB db = mongo.getDB("mytestdb");

      // Get the collection from MongoDB
      DBCollection collection = db.getCollection("students");

      // Search query
      System.out.println("--- All Documents ---");
      BasicDBObject searchQuery = new BasicDBObject();
      printResults(collection, searchQuery);
      
      System.out.println("--- Results for name like '^Sri'---");
      searchQuery = new BasicDBObject();
      searchQuery.put("name", java.util.regex.Pattern.compile("^Sri"))//starts with 'Sri'
      printResults(collection, searchQuery);
      
      System.out.println("--- Results for name like 'kar$'---");
      searchQuery = new BasicDBObject();
      searchQuery.put("name", java.util.regex.Pattern.compile("kar$"))//ends with 'kar'
      printResults(collection, searchQuery);
      
      System.out.println("--- Results for name like '.*a.*'---");
      searchQuery = new BasicDBObject();
      searchQuery.put("name", java.util.regex.Pattern.compile(".*a.*"))//contains 'a'
      printResults(collection, searchQuery);

    catch (UnknownHostException e) {
      e.printStackTrace();
    catch (MongoException e) {
      e.printStackTrace();
    }
  }
  
  private static void printResults(DBCollection collection, BasicDBObject searchQuery) {
    
    DBCursor cursor = collection.find(searchQuery);
    
    // loop over the cursor and display the retrieved result
    while (cursor.hasNext()) {
      System.out.println(cursor.next());
    }
    System.out.println();
  }
}
   

It gives the following output,
--- All Documents ---
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7d"} , 
	"name" : "Sriram" , "age" : 2}
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7e"} , 
	"name" : "Sudhakar" , "age" : 29 , "mobile" : "123456789"}

--- Results for name like '^Sri'---
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7d"} , 
	"name" : "Sriram" , "age" : 2}

--- Results for name like 'kar$'---
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7e"} , 
	"name" : "Sudhakar" , "age" : 29 , "mobile" : "123456789"}

--- Results for name like '.*a.*'---
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7d"} , 
	"name" : "Sriram" , "age" : 2}
{ "_id" : { "$oid" : "503ccca542e261e1f1f0db7e"} , 
	"name" : "Sudhakar" , "age" : 29 , "mobile" : "123456789"}



 
  


  
bl  br