標籤:des style blog http ar io color os 使用
對於mongoDB而言,學習方式和學習關係型資料庫差不太多
開始都是學習怎樣insert、find、update、remove,然後就是分頁、排序、索引,再接著就是主從複製、複本集、分區等等
最後就是通過它提供的各個驅動(比方Java、PHP、node.js等等)來練習所謂的進階使用方法
另外:相對於Oracle有PLSQL Developer,MySQL有HeidiSQL
mongoDB也不例外,它的圖形化工具中有一款叫做mongoVUE的,我用的是1.5.3破解版(不是每15天就得改一次注冊表的那種)
以下示範的就是以Java為例,常見的操作mongoDB API使用方法
package com.jadyer.test;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.List;import org.bson.types.ObjectId;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.MongoClient;import com.mongodb.WriteResult;import com.mongodb.util.JSON;/** * mongoDB的Java驅動測試 * @see ---------------------------------------------------------------------------------------- * @see 配置 * @see 1)下載https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.1.zip * @see 2)解壓到本地硬碟D:\Develop\mongoDB\中,並配置環境變數path=D:\Develop\mongoDB\bin * @see 然後在CMD下運行此命令驗證成功安裝與否>mongod --version * @see 3)建立D:\Develop\mongoDBData\目錄,用於存放mongoDB資料檔案 * @see 4)自己定義bat檔案,分別用於啟動mongoDB資料庫和串連資料庫的client * @see 啟動client的mongo_client.bat內容為-->mongo 127.0.0.1:27017/admin * @see 啟動資料庫的mongo_db.bat內容為------>mongod --dbpath D:\Develop\mongoDBData --rest * @see 注:加入[--rest]參數是為了可以訪問mongoDB的Web控制台http://127.0.0.1:28017/ * @see ---------------------------------------------------------------------------------------- * @see Java驅動 * @see 這裡要用到mongoDB的Java驅動包,http://docs.mongodb.org/ecosystem/drivers/java/ * @see ---------------------------------------------------------------------------------------- * @create May 15, 2014 10:17:30 PM * @author 玄玉<http://blog.csdn.net/jadyer> */public class MongoDBTest {private static MongoClient mongoClient;private static DB db;/** * 建立資料庫連接 */@BeforeClasspublic static void globalInit(){try {//mongoClient = new MongoClient(Arrays.asList(new ServerAddress("127.0.0.1", 27017), new ServerAddress("127.0.0.1", 27018), new ServerAddress("127.0.0.1", 27019)));mongoClient = new MongoClient("127.0.0.1", 27017);} catch (UnknownHostException e) {System.err.println("mongoDB主機地址有誤");}db = mongoClient.getDB("mydemo");}/** * 銷毀資料庫連接 */@AfterClasspublic static void globalDestroy(){mongoClient.close();}/** * 擷取資料庫資訊 */@Testpublic void getMetaData(){//查詢資料庫中全部的集合名稱for(String collectionName : db.getCollectionNames()){System.out.println("mydemo資料庫所擁有的集合為:[" + collectionName + "]");}//查詢某一集合中的資料DBCollection collection = db.getCollection("person");DBCursor cursor = collection.find();try{while(cursor.hasNext()){System.out.println("person集合所擁有的name為--[" + cursor.next().get("name") + "]");}}finally{cursor.close();}System.out.println("person集合中的記錄數為----------->" + cursor.count());System.out.println("person集合資料格式化後的JSON串為-->" + JSON.serialize(cursor));}/** * 建立一個空的"moive"集合 */@Testpublic void createCollection(){db.createCollection("movie", new BasicDBObject());}/** * 為"moive"集合加入文檔 */@Testpublic void insertDocument(){DBObject doc = new BasicDBObject();doc.put("name", "24");doc.put("season", "ninth");doc.put("score", 88);List<String> actorList = new ArrayList<String>();actorList.add("Jack Bauer");actorList.add("Counter Terrorist Unit");doc.put("actor", actorList);db.getCollection("movie").insert(doc);}/** * 批量插入文檔 */@Testpublic void insertBatchDocument(){List<DBObject> docList = new ArrayList<DBObject>();DBObject doc11 = new BasicDBObject("name", "Prison Break").append("season", "fourth").append("score", 99);DBObject doc22 = new BasicDBObject("name", "Game of Thrones").append("season", "third").append("score", 92);docList.add(doc11);docList.add(doc22);db.getCollection("movie").insert(docList);}/** * 依據_id刪除資料 */@Testpublic void deleteById(){WriteResult result = db.getCollection("movie").remove(new BasicDBObject("_id", new ObjectId("5374c6dc5030e0ea4dac8907")));System.out.println("本次操作影響的記錄條數為:" + result.getN());}/** * 依據條件刪除資料 */@Testpublic void deleteByData(){DBObject doc = new BasicDBObject();doc.put("name", "Prison Break");WriteResult result = db.getCollection("movie").remove(doc);System.out.println("本次操作影響的記錄條數為:" + result.getN());}/** * 更新資料 * @see 添加email屬性 */@Testpublic void update(){DBObject doc = new BasicDBObject();doc.put("$set", new BasicDBObject("email", "[email protected]"));WriteResult result = db.getCollection("movie").update(new BasicDBObject(), doc, false, true);System.out.println("本次操作影響的記錄條數為:" + result.getN());}/** * 查詢"moive"集合中的key */@Testpublic void getKey(){DBObject keys = new BasicDBObject();keys.put("_id", false);keys.put("name", true);//keys.put("score", true);//第一個參數表示查詢條件,第二個參數表示返回的詳細keyDBCursor cursor = db.getCollection("movie").find(null, keys);try{while(cursor.hasNext()){DBObject object = cursor.next();System.out.println("查詢到的name=" + object.get("name") + ", score=" + object.get("score"));}}finally{cursor.close();}}/** * 查詢"moive"集合中分數不超過95的key */@Testpublic void getKeyUseScore(){DBObject ref = new BasicDBObject();ref.put("score", new BasicDBObject("$lte", 95));DBCursor cursor = db.getCollection("movie").find(ref, null);try{while(cursor.hasNext()){DBObject object = cursor.next();System.out.println("查詢到的name=" + object.get("name") + ", score=" + object.get("score"));}}finally{cursor.close();}}/** * 分頁查詢 */@Testpublic void limitSkip(){DBCursor cursor = db.getCollection("movie").find(null, null);cursor.limit(0).skip(1);try{while(cursor.hasNext()){DBObject object = cursor.next();System.out.println("查詢到的name=" + object.get("name"));}}finally{cursor.close();}}}
Java操作mongoDB2.6的常見API使用方法