本文轉自http://blog.csdn.net/mydeman/article/details/6652387java
在MongoDB的官方文檔中關於Java操做的介紹,只給出了很簡單的幾個例子。這些例子雖然能夠知足必定的需求,可是還並非太徹底。下面是我根據網頁中的提示寫的幾個例子。mongodb
1.背景。用JUnit4.8.2實現的單元測試的形式。測試數據:數據庫
[plain] view plaincopyprint?單元測試
{uid:10,username:"Jim",age:23,agender:"male"}測試
{uid:27,username:"tom",age:13,agender:"male"}ui
{uid:12,username:"Jane",age:31,agender:"female"}spa
{uid:23,username:"Alex",age:47,agender:"male"}.net
{uid:109,username:"Lily",age:24,agender:"female"}code
{uid:10,username:"Jim",age:23,agender:"male"} {uid:27,username:"tom",age:13,agender:"male"} {uid:12,username:"Jane",age:31,agender:"female"} {uid:23,username:"Alex",age:47,agender:"male"} {uid:109,username:"Lily",age:24,agender:"female"}
單元測試的初始化和清理工做,主要是創建數據庫鏈接、寫入測試數據、清理測試數據:blog
[java] view plaincopyprint?
private static List<BasicDBObject> documents = new ArrayList<BasicDBObject>();
private static DBCollection coll;
@BeforeClass
public static void init(){
try {
initConnection();
loadData();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void initConnection() throws UnknownHostException, MongoException{
//Create a connection to Collection 'user'
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("test");
coll = db.getCollection("user");
}
private static void loadData() throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(MongoTest.class.getResourceAsStream("data")));
String line = null;
while((line = br.readLine()) != null){
JSONObject jo = new JSONObject(line);
//Convert JSONObject into BasicDBObject
BasicDBObject dbObject = new BasicDBObject();
Iterator<String> joKeys = jo.keys();
while(joKeys.hasNext()){
String key = joKeys.next();
dbObject.put(key, jo.get(key));
}
documents.add(dbObject);
}
}
@Before
public void setUp(){
//Insert all data into MongoDB
for(BasicDBObject bdo : documents){
coll.insert(bdo);
}
}
public void cleanUp(){
//Drop the collection to remove all data.
//Note: it's not recommended.
coll.drop();
}
private static List<BasicDBObject> documents = new ArrayList<BasicDBObject>(); private static DBCollection coll; @BeforeClass public static void init(){ try { initConnection(); loadData(); } catch (Exception e) { e.printStackTrace(); } } private static void initConnection() throws UnknownHostException, MongoException{ //Create a connection to Collection 'user' Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("test"); coll = db.getCollection("user"); } private static void loadData() throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(MongoTest.class.getResourceAsStream("data"))); String line = null; while((line = br.readLine()) != null){ JSONObject jo = new JSONObject(line); //Convert JSONObject into BasicDBObject BasicDBObject dbObject = new BasicDBObject(); Iterator<String> joKeys = jo.keys(); while(joKeys.hasNext()){ String key = joKeys.next(); dbObject.put(key, jo.get(key)); } documents.add(dbObject); } } @Before public void setUp(){ //Insert all data into MongoDB for(BasicDBObject bdo : documents){ coll.insert(bdo); } } @After public void cleanUp(){ //Drop the collection to remove all data. //Note: it's not recommended. coll.drop(); }
2. AND是比較簡單的。
[java] view plaincopyprint?
public void testAnd(){
//agender='female' AND age > 27
DBObject queryCondition = new BasicDBObject();
queryCondition.put("agender", "female");
queryCondition.put("age", new BasicDBObject("$gt", 27));
DBCursor dbCursor = coll.find(queryCondition);
assertEquals(1, dbCursor.size());
assertEquals("Jane", dbCursor.next().get("username"));
}
@Test public void testAnd(){ //agender='female' AND age > 27 DBObject queryCondition = new BasicDBObject(); queryCondition.put("agender", "female"); queryCondition.put("age", new BasicDBObject("$gt", 27)); DBCursor dbCursor = coll.find(queryCondition); assertEquals(1, dbCursor.size()); assertEquals("Jane", dbCursor.next().get("username")); }
3.單個字段的OR操做。
[java] view plaincopyprint?
public void testOrSingleField(){
DBObject queryCondition = new BasicDBObject();
//age<15 OR age>27
queryCondition = new BasicDBObject();
BasicDBList values = new BasicDBList();
values.add(new BasicDBObject("age", new BasicDBObject("$gt", 27)));
values.add(new BasicDBObject("age", new BasicDBObject("$lt", 15)));
queryCondition.put("$or", values);
DBCursor dbCursor = coll.find(queryCondition);
assertEquals(3, dbCursor.size());
assertEquals("tom", dbCursor.next().get("username"));
}
@Test public void testOrSingleField(){ DBObject queryCondition = new BasicDBObject(); //age<15 OR age>27 queryCondition = new BasicDBObject(); BasicDBList values = new BasicDBList(); values.add(new BasicDBObject("age", new BasicDBObject("$gt", 27))); values.add(new BasicDBObject("age", new BasicDBObject("$lt", 15))); queryCondition.put("$or", values); DBCursor dbCursor = coll.find(queryCondition); assertEquals(3, dbCursor.size()); assertEquals("tom", dbCursor.next().get("username")); }
4. 多個字段之間的OR操做
[java] view plaincopyprint?
@Test
public void testOrMultiFields(){
DBObject queryCondition = new BasicDBObject();
//agender=female OR age<=23
queryCondition = new BasicDBObject();
BasicDBList values = new BasicDBList();
values.add(new BasicDBObject("agender", "female"));
values.add(new BasicDBObject("age", new BasicDBObject("$lte", 23)));
queryCondition.put("$or", values);
DBCursor dbCursor = coll.find(queryCondition);
assertEquals(4, dbCursor.size());
assertEquals("Jim", dbCursor.next().get("username"));
}
@Test public void testOrMultiFields(){ DBObject queryCondition = new BasicDBObject(); //agender=female OR age<=23 queryCondition = new BasicDBObject(); BasicDBList values = new BasicDBList(); values.add(new BasicDBObject("agender", "female")); values.add(new BasicDBObject("age", new BasicDBObject("$lte", 23))); queryCondition.put("$or", values); DBCursor dbCursor = coll.find(queryCondition); assertEquals(4, dbCursor.size()); assertEquals("Jim", dbCursor.next().get("username")); }
5. 單個字段的IN操做。對於相似 where age=13 OR age=47的查詢條件,就能夠考慮使用IN代替
[java] view plaincopyprint?
@Test
public void testIn(){
DBObject queryCondition = new BasicDBObject();
//age in [13, 47]
queryCondition = new BasicDBObject();
BasicDBList values = new BasicDBList();
values.add(13);
values.add(47);
queryCondition.put("age", new BasicDBObject("$in", values));
DBCursor dbCursor = coll.find(queryCondition);
assertEquals(2, dbCursor.size());
assertEquals("tom", dbCursor.next().get("username"));
}
@Test public void testIn(){ DBObject queryCondition = new BasicDBObject(); //age in [13, 47] queryCondition = new BasicDBObject(); BasicDBList values = new BasicDBList(); values.add(13); values.add(47); queryCondition.put("age", new BasicDBObject("$in", values)); DBCursor dbCursor = coll.find(queryCondition); assertEquals(2, dbCursor.size()); assertEquals("tom", dbCursor.next().get("username")); }
從以上幾個例子能夠看出,經過BasicDBList與BasicDBObject的相結合能夠得出比較複雜的查詢條件。