首先,下載mongdb對JAVA的支持,點擊這裏下載驅動包,這裏博主下載的是2.10.1版。java
mongdb版本爲2.4.9
git
在項目中導入該驅動包。github
打開shell,在終端輸入mongo,顯示以下正則表達式
只有默認的三個數據庫。mongodb
新建一個java projectshell
代碼以下,數據庫
1.操控mongdb鏈接,使用MongoOptions來設置一些參數。app
package Util; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.Mongo; import com.mongodb.MongoOptions; //數據庫工具類 public class MongoDBUtil { private static Mongo mongo = null; private static String DBString = "MongoDBTest";//數據庫名 private static String hostName = "localhost";//主機名 private static int port = 27017;//端口號 private static int poolSize = 10;//鏈接池大小 private MongoDBUtil(){ } //獲取數據庫鏈接 public static DB getDB(){ if(mongo == null){ init(); } return mongo.getDB(DBString); } //初始化數據庫 private static void init(){ try { //實例化Mongo mongo = new Mongo(hostName, port); MongoOptions opt = mongo.getMongoOptions(); //設置鏈接池大小 opt.connectionsPerHost = poolSize; } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.基本操做接口ide
package DAO; import java.util.List; import com.mongodb.BasicDBObject; //數據庫CRUD基本操做 public interface BaseDAO { public boolean insert(String collectionName, BasicDBObject bean); public boolean delete(String collectionName, BasicDBObject bean); public List find(String collectionName, BasicDBObject bean); public boolean update(String collectionName, BasicDBObject oldBean, BasicDBObject newBean); }
3.基本操做實現工具
package DAO.Impl; import java.util.List; import com.mongodb.BasicDBObject; import com.mongodb.DB; import Util.MongoDBUtil; import DAO.BaseDAO; //數據庫基本操做實現 public class BaseDAOImpl implements BaseDAO { @Override public boolean insert(String collectionName, BasicDBObject bean) { DB db = MongoDBUtil.getDB(); db.getCollection(collectionName).insert(bean); return false; } @Override public boolean delete(String collectionName, BasicDBObject bean) { DB db = MongoDBUtil.getDB(); db.getCollection(collectionName).remove(bean); return false; } @Override public List find(String collectionName, BasicDBObject bean) { DB db = MongoDBUtil.getDB(); List list = db.getCollection(collectionName).find(bean).toArray(); return list ; } @Override public boolean update(String collectionName, BasicDBObject oldBean, BasicDBObject newBean) { DB db = MongoDBUtil.getDB(); db.getCollection(collectionName).update(oldBean, newBean); return false; } }
4.基本操做測試
4.1添加文檔操做
BaseDAOImpl baseDAOImpl = new BaseDAOImpl(); BasicDBObject beanOne = new BasicDBObject(); beanOne.put("name", "kakakaka"); beanOne.put("sex", "男"); beanOne.put("age", 20); baseDAOImpl.insert("test", beanOne);
執行後結果
能夠看到mongdb會自動檢測是否存在數據庫MongoDBTest,若是不存在則新建一個,而後再檢測是否存在文檔集合test,若是不存在則新建一個,最後插入數據。
4.2查詢文檔操做
List<BasicDBObject> list = baseDAOImpl.find("test", new BasicDBObject("name", "kakakaka")); for(BasicDBObject i : list){ System.out.println(i.get("name")); System.out.println(i.get("sex")); System.out.println(i.get("age")); }
輸出結果以下
kakakaka 男 20
4.3修改文檔操做以下
BasicDBObject oldBean = (BasicDBObject) baseDAOImpl.find("test", new BasicDBObject("name", "kakakaka")).get(0); BasicDBObject newBean = (BasicDBObject) oldBean.clone(); newBean.put("name", "gugugugu"); System.out.println(oldBean.get("name")); System.out.println(newBean.get("name")); baseDAOImpl.update("test", oldBean, newBean);
控制檯輸出結果
kakakaka gugugugu
4.4刪除文檔
先再向數據庫插入一條數據
注意,mongdb區分一個文檔中的不一樣數據是依賴它本身向每條數據中賦的"_id"
因此再插入這樣一條數據
BaseDAOImpl baseDAOImpl = new BaseDAOImpl(); BasicDBObject beanOne = new BasicDBObject(); beanOne.put("name", "gugugugu"); beanOne.put("sex", "男"); beanOne.put("age", 20); baseDAOImpl.insert("test", beanOne);
結果以下
執行刪除操做
首先根據"_id"將文檔中第二條數據修改
baseDAOImpl.update("test", new BasicDBObject("_id", new ObjectId("5471c9db44aeeb8b5524f2ea")) , new BasicDBObject("name","kakakaka").append("age", 20));
結果以下
將"name" 等於 "kakakaka"的數據刪除
baseDAOImpl.delete("test", new BasicDBObject("name","kakakaka"));
5.模糊搜索
使用正則表達式根據關鍵字key對數據庫相應屬性進行模糊搜索,
String key = "a"; Pattern pattern = Pattern.compile("^.*" + key + ".*$", Pattern.CASE_INSENSITIVE); DB db = MongoDBUtil.getDB(); List list = db.getCollection("test").find(new BasicDBObject("name", pattern)).toArray(); for(int i = 0; i < list.size();i++){ System.out.println(list.get(i)); }
數據庫內部的內容
輸出結果
{ "_id" : { "$oid" : "547ebfc744aefdcc5fe90bce"} , "name" : "kakakaka" , "sex" : "男" , "age" : 20}