1. 程式人生 > >mongodb之java基本操作

mongodb之java基本操作

mongodb由於其儲存大量資料,支援高併發,弱事務的特性,受到很多程式人的喜歡。

對於java來說需要下載java-mongo的驅動包,類似mysql驅動包,裡面包含java操作mongo的工具類。

驅動包版本為3.0以下的使用DBCollection。。。
驅動包版本為3.0以上的使用MongoCollection Document。。。

本人習慣使用3.0版本以下的,如下:

DBCollection類:指資料庫中的集合,提供獲取集合表的基本方法,在關係性資料庫來說是表,非關係性資料庫就是集合;

DBObject類:指集合中的一個文件,通俗來講,資料庫表中的一條記錄值;

DBCursor類:

指遊標返回的文件集合。

使用:

連線服務

        //連線到mongodb服務
        Mongo mongo=new MongoClient("10.20.8.121",20000);
        //連線到資料庫
        DB db=mongo.getDB("myMongoDB");
        //對myMongoDB資料庫中course集合進行操作 
        DBCollection course = db.getCollection("course");

插入

插入單條資料,主要有四種方式:
1.DBObject .append() 2. DBObject .put() 3. 通過map 4. 將json轉換成DBObject物件
插入批量資料使用list

//第一種append
DBObject english = new BasicDBObject().append("name","english").append("score", 5).append("id",1);  
course.insert(english);  
//第二種put
DBObject math = new BasicDBObject();  
math.put("id", 2);  
math.put("name", "math");  
math.put("score", 10);  
course.insert(math);  
//第三種map    
Map<String,Object> map
= new HashMap<String,Object>(); map.put("name","physics" ); map.put("score", 10); map.put("id", 3); DBObject physics= new BasicDBObject(map); course.insert(physics); //第四種json String json ="{'name':'chemistry','score':10,'id':4}"; DBObject chemistry =(DBObject)JSON.parse(json); course.insert(chemistry); //第五種批量插入多條記錄 List<DBObject> courseList = new ArrayList<DBObject>(); DBObject chinese = new BasicDBObject().append("name","chinese").append("score", 10).append("id", 5); DBObject history = new BasicDBObject().append("name", "history").append("score", 10).append("id", 6); courseList.add(chinese); courseList.add(history); course.insert(courseList); //新增內嵌文件 String json2 =" {'name':'english','score':10,'teacher':[{'name':'柳鬆','id':'1'},{'name':'柳鬆鬆','id':2}]}"; DBObject english2= (DBObject)JSON.parse(json); course.insert(english2); List<DBObject> list = new ArrayList<DBObject>(); list.add(new BasicDBObject("name","柳鬆").append("id",1)); list.add(new BasicDBObject("name","柳鬆鬆").append("id",2)); DBObject english3= new BasicDBObject().append("name","english").append("score",10).append("teacher",list);

查詢

這裡只列出基本的查詢所有,單個查詢,條件查詢等,高階聚合查詢統計見下一篇部落格

//查詢所  
DBCursor cur = course.find();  
while(cur.hasNext()){  
      DBObject document = cur.next();  
      System.out.println(document.get("name"));  
  }  
//查詢一個文件       
DBObject document = course.findOne();  
String name=(String)document.get("name");  
System.out.println(name);  


//有條件查詢 查詢學分=5的  
DBObject query1 = new BasicDBObject("score",5);  
//查詢學分大於5的
DBObject query2 = new BasicDBObject("score",new BasicDBObject("$gte",5));  
DBCursor cur2 = course.find(query2);  

//條件表示式:$ge(>)  $get(>=)  $lt(<)  $lte(<=)  $ne(<>)  $in  $nin  $all $exists $or  $nor $where $type等等  


//查詢並修改       
DBObject newDocument = course.findAndModify(new BasicDBObject("score",5), new BasicDBObject("score",15));  

修改

//q:更新條件  o:更新後的物件  
course.update(new BasicDBObject("score",10), new BasicDBObject("test",15));  
course.update(new BasicDBObject("score",15), new BasicDBObject("$set",new BasicDBObject("isRequired",true)));  
//兩個的區別是,第一個更新是將{"test":15}這個文件替換原來的文件,  
//第二個更新添加了條件表示式$set,是在原來文件的基礎上新增"isRequired"這個鍵  

//條件表示式:$set  $unset  $push  $inc  $push $push  $addToSet  $pull $pullAll  $pop等等  

//當_id相同時,執行save方法相當於更新操作  
course.save(new BasicDBObject("name","math").append("_id", 1));  
course.save(new BasicDBObject("name","數學").append("_id", 1));  

刪除

//刪除符合條件的文件  
course.remove(new BasicDBObject("score",15));  

//刪除集合及所有文件  
course.drop();