//Your need mongoDB driver for this!!!!!!!!!!!
package second;
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.Random;
public class Query {
public static void main(String sf[]) throws UnknownHostException {
//connect to DB on localhost on default port (27017)
MongoClient client = new MongoClient();
//select DB "course"
DB courseDB = client.getDB("course");
//use collection "queryTests"
DBCollection collection = courseDB.getCollection("queryTests");
//cleaning collection if it is not empty
collection.drop();
//populate collection with such kind objects: { "x" : 0 , "y" : 62}
for (int i = 0; i < 10; i++) {
collection.insert(new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100)));
}
// for example I want to find all objects that have x=0; 10>=y>=90
DBObject search = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gte", 10).append("$lte", 90));
System.out.println("Count from BasicDBObject");
//just counting such objects
long k = collection.count(search);
System.out.println(k);
System.out.println("Find from BasicDBObject");
//ask to find all objects that suits "search" BasicDBObject patter, and ak not to retrieve their id's
DBCursor cursor = collection.find(search, new BasicDBObject("_id", false));
//print the cursor data
showDataFromCursor(cursor);
//the fancy way to do the same is criteria builder, doing absolutely the same (finds all objects that have x=0; 10>=y>=90)
QueryBuilder queryBuilder = QueryBuilder.start("x").is(0).and("y").greaterThanEquals(10).lessThanEquals(90);
System.out.println("Count from Criteria");
long j = collection.count(queryBuilder.get());
System.out.println(j);
System.out.println("Find from Criteria");
DBCursor cursor1 = collection.find(queryBuilder.get(), new BasicDBObject("_id", false));
showDataFromCursor(cursor1);
}
//method that prints out data from cursor
public static void showDataFromCursor(DBCursor cursor) {
try {
while (cursor.hasNext()) {
DBObject cur = cursor.next();
System.out.println(cur);
}
} finally {
cursor.close();
}
}
}
package second;
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.Random;
public class Query {
public static void main(String sf[]) throws UnknownHostException {
//connect to DB on localhost on default port (27017)
MongoClient client = new MongoClient();
//select DB "course"
DB courseDB = client.getDB("course");
//use collection "queryTests"
DBCollection collection = courseDB.getCollection("queryTests");
//cleaning collection if it is not empty
collection.drop();
//populate collection with such kind objects: { "x" : 0 , "y" : 62}
for (int i = 0; i < 10; i++) {
collection.insert(new BasicDBObject("x", new Random().nextInt(2)).append("y", new Random().nextInt(100)));
}
// for example I want to find all objects that have x=0; 10>=y>=90
DBObject search = new BasicDBObject("x", 0).append("y", new BasicDBObject("$gte", 10).append("$lte", 90));
System.out.println("Count from BasicDBObject");
//just counting such objects
long k = collection.count(search);
System.out.println(k);
System.out.println("Find from BasicDBObject");
//ask to find all objects that suits "search" BasicDBObject patter, and ak not to retrieve their id's
DBCursor cursor = collection.find(search, new BasicDBObject("_id", false));
//print the cursor data
showDataFromCursor(cursor);
//the fancy way to do the same is criteria builder, doing absolutely the same (finds all objects that have x=0; 10>=y>=90)
QueryBuilder queryBuilder = QueryBuilder.start("x").is(0).and("y").greaterThanEquals(10).lessThanEquals(90);
System.out.println("Count from Criteria");
long j = collection.count(queryBuilder.get());
System.out.println(j);
System.out.println("Find from Criteria");
DBCursor cursor1 = collection.find(queryBuilder.get(), new BasicDBObject("_id", false));
showDataFromCursor(cursor1);
}
//method that prints out data from cursor
public static void showDataFromCursor(DBCursor cursor) {
try {
while (cursor.hasNext()) {
DBObject cur = cursor.next();
System.out.println(cur);
}
} finally {
cursor.close();
}
}
}
No comments:
Post a Comment