日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

mongodb java 学习_MongoDB学习(四):通过Java使用MongoDB

發(fā)布時間:2025/5/22 java 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb java 学习_MongoDB学习(四):通过Java使用MongoDB 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

環(huán)境配置

在Java項目中使用MongoDB,需要在項目中引入mongo.jar這個包。下載地址:下載

請盡量下載較新的版本,本文用的是2.10.1。

連接MongoDB

1 public synchronized static DB getDBConnection() throwsUnknownHostException{2 if(db == null){3 MongoClient client = newMongoClient(DB_SERVER_IP, DBSERVER_PORT);4 db =client.getDB(DB_NAME);5 System.out.println("GET DBCONNECTION SUCCESSFUL");6 }7 returndb;8 }

其中IP和PORT分別是數(shù)據(jù)庫服務(wù)端的IP和端口號。

創(chuàng)建集合

public staticDBCollection getCollection(String colName){

col=db.getCollection(colName);if(col == null){

col= db.createCollection(colName, null);

}returncol;

}

插入文檔

插入數(shù)據(jù)有4中方式可選:1.利用DBObjcet,2.利用DBObjectBuilder, 3.先創(chuàng)建Map對象,再用Map對象構(gòu)造DBObject,4.直接通過json對象創(chuàng)建。

這里我們主要介紹第一種方式——利用DBObject插入。DBObject是一個接口,繼承自BSONObject,是可被存入數(shù)據(jù)庫的一個鍵值的Map。這里我們使用它的基本實現(xiàn):BasicDBObject。

public static voidinsert(){

DBCollection col= getCollection("myCollection1");if(col != null){

DBObject o= newBasicDBObject();

o.put("name", "Z");

o.put("gender", "f");

o.put("age", 1);

col.insert(o);

}

}

另外,你也可以使用BasicDBObject提供的append()函數(shù),來為對象插入鍵值對。

當你需要批量插入數(shù)據(jù)時,可以使用DBCollection.insert(List list);

查詢文檔

可以通過DBCollection.find()來查詢集合中的文檔。該函數(shù)返回一個游標DBCursor。通過對其迭代輸出,就可以得到文檔組。或者是通過DBCursor.toArray()直接轉(zhuǎn)成DBObject的列表List。代碼如下:

public static voidsearch(){

DBCollection col= getCollection("myCollection1");if(col != null){

DBCursor cursor=col.find();while(cursor.hasNext()){

DBObject o=cursor.next();

System.out.println(o);

}

}

}

public static voidsearch(){

DBCollection col= getCollection("myCollection1");if(col != null){

List list =col.find().toArray();

}

}

當然,也可以設(shè)置查詢條件,并對輸出結(jié)果的字段進行限制:

public static voidsearch2(){

DBCollection col= getCollection("myCollection1");if(col != null){//查詢條件

DBObject query = newBasicDBObject();

query.put("gender", "m");//輸出結(jié)果是否有要輸出某個字段。0表示不輸出,1表示只輸出該字段

DBObject field = newBasicDBObject();

field.put("name", 1);

DBCursor cursor=col.find(query,field);while(cursor.hasNext()){

System.out.println(cursor.next());

}

}

}

上述代碼就表示查詢性別為m的全部文檔,并輸出其姓名。

較復(fù)雜的條件查詢:

public static voidsearch2(){

DBCollection col= getCollection("myCollection1");if(col != null){//查詢條件

DBObject query = newBasicDBObject();

DBObject o= new BasicDBObject("$lt",24).append("$gt", 21);

query.put("age", o);

DBCursor cursor=col.find(query);while(cursor.hasNext()){

System.out.println(cursor.next());

}

}

}

這里我們篩選的是年齡小于24且大于21的全部文檔。

更新文檔

public static voidupdate1(){

DBCollection col= getCollection("myCollection1");if(col != null){//查詢條件

DBObject query = newBasicDBObject();

query.put("name", "A");//用來替換的文檔

DBObject newObject = newBasicDBObject();

newObject.put("name", "A1");

col.update(query, newObject);

}

}

這里我們將name這個字段等于A的文檔替換成了name字段為A1的值,注意的是,新的文檔將不包含舊文檔的其他字段,是真正意義上的兩個文檔的替換,而非替換相同字段!另外一點需要注意的是,該方法只替換第一條符合查詢條件的文檔。因為multi的默認值為false,可以通過設(shè)置這個值為true來修改多條。

findAndModity(DBObject?query,??DBObject??fields,?DBObject?sort,? boolean?remove,?DBObject?update,boolean?returnNew,boolean?upsert)也提供了類似的功能。

query?- query to match

fields?- fields to be returned

sort?- sort to apply before picking first document

remove?- if true, document found will be removed

update?- update to apply

returnNew?- if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever)upsert?- do upsert (insert if document not present)

刪除文檔

刪除指定的一個文檔:

public static voidremove1(){

DBCollection col= getCollection("myCollection1");if(col != null){

DBObject o=col.findOne();

col.remove(o);

}

}

刪除符合某條件的文檔:

public static voidremove2(){

DBCollection col= getCollection("myCollection1");if(col != null){//條件

DBObject query = newBasicDBObject();

query.put("name", "A1");

col.remove(query);

}

}

會刪除符合條件的全部文檔。

如需要刪除集合下的全部文檔時,可結(jié)合DBCursor實現(xiàn)。

參考:

其他資料:菜鳥教程

總結(jié)

以上是生活随笔為你收集整理的mongodb java 学习_MongoDB学习(四):通过Java使用MongoDB的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。