package com.mkyong.core;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* Java MongoDB : Query document
*
* @author mkyong
*
*/
public class QueryApp {
public static void insertDummyDocuments(DBCollection collection) {
List<DBObject> dbObjects = new ArrayList<DBObject>();
Calendar calendar = Calendar.getInstance();
for (int i = 1; i <= 5; i++) {
BasicDBObject data = new BasicDBObject();
data.append("number", i);
data.append("name", "t4b-doc-" + i);
data.append("date", calendar.getTime());
calendar.add(Calendar.DATE, 1);
dbObjects.add(data);
}
collection.insert(dbObjects);
}
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("127.0.0.1", 27017);
DB db = mongo.getDB("t4b_demodb");
// get collection by name
DBCollection collection = db.getCollection("demo_collection");
// insert dummy documents in the collection
insertDummyDocuments(collection);
System.out.println("Get first matched document");
DBObject dbObject = collection.findOne();
System.out.println(dbObject);
System.out.println("Get all matched documents");
DBCursor cursor1 = collection.find();
while (cursor1.hasNext()) {
System.out.println(cursor1.next());
}
System.out.println("Get 'name' field only");
BasicDBObject allQuery = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();
fields.put("name", 1);
DBCursor cursor2 = collection.find(allQuery, fields);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
System.out.println("Get where number = 5");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("number", 5);
DBCursor cursor3 = collection.find(whereQuery);
while (cursor3.hasNext()) {
System.out.println(cursor3.next());
}
System.out.println("\nFind where number in 2 and 4");
BasicDBObject inQuery = new BasicDBObject();
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(4);
inQuery.put("number", new BasicDBObject("$in", list));
DBCursor cursor4 = collection.find(inQuery);
while (cursor4.hasNext()) {
System.out.println(cursor4.next());
}
System.out.println("Get where 2 < number < 5");
BasicDBObject gtQuery = new BasicDBObject();
gtQuery.put("number", new BasicDBObject("$gt", 2).append("$lt", 5));
DBCursor cursor5 = collection.find(gtQuery);
while (cursor5.hasNext()) {
System.out.println(cursor5.next());
}
System.out.println("Get where number != 4");
BasicDBObject neQuery = new BasicDBObject();
neQuery.put("number", new BasicDBObject("$ne", 4));
DBCursor cursor6 = collection.find(neQuery);
while (cursor6.hasNext()) {
System.out.println(cursor6.next());
}
System.out.println("Get when number = 2 and name = 't4b-doc-2' example");
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("number", 2));
obj.add(new BasicDBObject("name", "t4b-doc-2"));
andQuery.put("$and", obj);
System.out.println(andQuery.toString());
DBCursor cursor7 = collection.find(andQuery);
while (cursor7.hasNext()) {
System.out.println(cursor7.next());
}
System.out.println("Get where name = 't4b.*-[1-3]', case sensitive example");
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("name", new BasicDBObject("$regex", "t4b.*-[1-3]").append("$options", "i"));
System.out.println(regexQuery.toString());
DBCursor cursor8 = collection.find(regexQuery);
while (cursor8.hasNext()) {
System.out.println(cursor8.next());
}
// collection.drop();
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
Java MongoDB Connection
Share This
Tags
# Java Database Connection
# Technology
Share This
About BunksAllowed
Technology
Labels:
Java Database Connection,
Technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.