對於MongoDB而言,學習方式和學習關系型數據庫差不太多
開始都是學習如何insert、find、update、remove,然後就是分頁、排序、索引,再接著就是主從復制、副本集、分片等等
最後就是通過它提供的各個驅動(比如Java、PHP、node.js等等)來練習所謂的高級用法
另外:相對於Oracle有PLSQL Developer,MySQL有HeidiSQL
mongoDB也不例外,它的圖形化工具中有一款叫做mongoVUE的,我用的是1.5.3破解版(不是每15天就得改一次注冊表的那種)
Linux下設置MongoDB開機自啟動 http://www.linuxidc.com/Linux/2011-07/39149.htm
CentOS編譯安裝MongoDB http://www.linuxidc.com/Linux/2012-02/53834.htm
CentOS 編譯安裝 MongoDB與mongoDB的php擴展 http://www.linuxidc.com/Linux/2012-02/53833.htm
CentOS 6 使用 yum 安裝MongoDB及服務器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm
Ubuntu 13.04下安裝MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm
MongoDB入門必讀(概念與實戰並重) http://www.linuxidc.com/Linux/2013-07/87105.htm
《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm
下面演示的就是以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數據庫和連接數據庫的客戶端
* @see 啟動客戶端的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;
/**
* 建立數據庫連接
*/
@BeforeClass
public 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");
}
/**
* 銷毀數據庫連接
*/
@AfterClass
public static void globalDestroy(){
mongoClient.close();
}
/**
* 獲取數據庫信息
*/
@Test
public 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"集合
*/
@Test
public void createCollection(){
db.createCollection("movie", new BasicDBObject());
}
/**
* 為"moive"集合添加文檔
*/
@Test
public 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);
}
/**
* 批量插入文檔
*/
@Test
public 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刪除數據
*/
@Test
public void deleteById(){
WriteResult result = db.getCollection("movie").remove(new BasicDBObject("_id", new ObjectId("5374c6dc5030e0ea4dac8907")));
System.out.println("本次操作影響的記錄條數為:" + result.getN());
}
/**
* 根據條件刪除數據
*/
@Test
public 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屬性
*/
@Test
public 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
*/
@Test
public void getKey(){
DBObject keys = new BasicDBObject();
keys.put("_id", false);
keys.put("name", true);
//keys.put("score", true);
//第一個參數表示查詢條件,第二個參數表示返回的具體key
DBCursor 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
*/
@Test
public 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();
}
}
/**
* 分頁查詢
*/
@Test
public 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();
}
}
}
MongoDB 的詳細介紹:請點這裡
MongoDB 的下載地址:請點這裡