當前位置:編程學習大全網 - 源碼下載 - 怎麽使用java操作mongodb更新整個文檔

怎麽使用java操作mongodb更新整個文檔

上篇博客介紹了java操作mongoDB進行對文件的處理。現在來介紹壹下對文檔的處理。和對文件的處理壹樣,也是通過java驅動中提供的幾個類相互作用完成的。這幾個類分別是:

DBCollection類:指定數據庫中指定集合的實例,提供了增刪改查等壹系列操作。在關系型數據庫中,對數據的增刪改查操作是建立在表的基礎上的,在mongodb中是建立在集合的基礎上進行的。

DBObject接口:DBObject是鍵值的映射,因此,可以將DBObject的實現類作為查詢的返回結果,也可以作為查詢條件

DBCursor:遊標,返回結果的集合。

下面是部分實例:

[java] view plaincopy

Mongo mongo = new Mongo();

DB db = mongo.getDB("myMongoDB");

DBCollection course = db.getCollection("course");//對myMongoDB數據庫中course集合進行操作

//添加操作

//下面分別是創建文檔的幾種方式:1. .append() 2. .put() 3. 通過map 4. 將json轉換成DBObject對象

DBObject english = new BasicDBObject().append("name","english").append("score", 5).append("id",1);

course.insert(english);

DBObject math = new BasicDBObject();

math.put("id", 2);

math.put("name", "math");

math.put("score", 10);

course.insert(math);

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);

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);

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();<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">

</span></span>

上面只是介紹了壹些簡單的操作,具體復雜的查詢更新可以根據需求再去查找文檔資料。其實,不管操作簡單還是復雜,其核心都是對DBObject和DBCollection的操作,主要掌握DBObject如何構造鍵值對,以及壹些條件表達式。

  • 上一篇:mysql 能否設置DDL語句 可以回滾?
  • 下一篇:格鬥遊戲的訣竅是什麽?
  • copyright 2024編程學習大全網