標籤:blog io os ar 使用 java for sp 資料
package mongo;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 connection {public static void main(String[] args){try{Mongo m = new Mongo("localhost", 27017);DB db = m.getDB("company");DBCollection collection = db.getCollection("employees");BasicDBObject employee = new BasicDBObject();employee.put("name", "Hannah");employee.put("no", 2);collection.insert(employee);BasicDBObject searchEmployee = new BasicDBObject();searchEmployee.put("no", 2);DBCursor cursor = collection.find(searchEmployee);while(cursor.hasNext()){System.out.println(cursor.next());}System.out.println("The Search Query has Executed!"); }catch(UnknownHostException e){e.printStackTrace();}catch(MongoException e){e.printStackTrace();}}}
{ "_id" : { "$oid" : "5454fa4458404d12637d418c"} , "name" : "Hannah" , "no" : 2}
The Search Query has Executed!
import java.net.UnknownHostException; import java.util.Set; 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; public class Test { /** * @author gaogao * @param args * @throws MongoException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, MongoException { // TODO Auto-generated method stub // 串連本機資料庫 Mongo m = new Mongo(); // 建立名為new_test_db的資料庫 DB db = m.getDB("new_test_db"); // 擷取new_test_db中的集合(類似於擷取關聯式資料庫中的表) Set<String> cols = db.getCollectionNames(); // 列印出new_test_db中的集合,這裡應當為null for (String s : cols) { System.out.println(s); } // 建立一個叫做"new_test_col"的集合 DBCollection collection = db.getCollection("new_test_col"); // 初始化一個基本DB對象,最終插入資料庫的就是這個DB對象 BasicDBObject obj = new BasicDBObject(); // 放入幾個索引值對 obj.put("from", "搞搞"); obj.put("to", "寶寶"); obj.put("subject", "狗子愛寶子"); //插入對象 collection.insert(obj); //查看一條記錄,findOne()=find().limit(1); DBObject dbobj=collection.findOne(); //列印出剛才插入的資料 System.out.println(dbobj); //現在我們來插入9條{ranking:i}的資料 for(int i=0;i<9;i++){ collection.insert(new BasicDBObject().append("ranking", i)); } //列印集合中的資料總數,這裡應當輸出10 System.out.println(collection.getCount()); //下面我們來遍曆集合,find()方法返回的是一個遊標(cursor),這裡的概念和關聯式資料庫很相似 DBCursor cursor=collection.find(); //然後我們使用這個遊標來遍曆集合 while(cursor.hasNext()){ System.out.println(cursor.next()); } //下面來看一些略複雜一點的查詢技巧,第一個,簡單的條件查詢,查詢ranking為1的記錄 BasicDBObject query=new BasicDBObject(); query.put("ranking", 1); cursor=collection.find(query); while(cursor.hasNext()){ System.out.println(cursor.next()); } //下面是更複雜的條件查詢,查詢ranking大於5小於9的記錄 query=new BasicDBObject(); query.put("ranking", new BasicDBObject("$gt", 5).append("$lt", 9)); cursor=collection.find(query); while(cursor.hasNext()){ System.out.println(cursor.next()); } //最後刪除我們的測試資料庫 m.dropDatabase("new_test_db"); } }
mongoDB用java實現增刪改查