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.
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);