一、mongoDB 資料庫系統的安裝和管理:
1,下載:http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.0.zip
2,解壓 mongodb-win32-i386-1.4.0.zip 到 D:/MongoDB/mongodbwin321.6.0/,再在其中建立檔案夾 data 存放測試資料庫用。
3,手工啟動 mingoDB Server:
開啟一個 DOS 視窗,進入 D:/MongoDB/mongodbwin321.6.0/bin 目錄,運行命令:
D:/MongoDB/mongodbwin321.6.0/bin>mongod --dbpath D:/MongoDB/mongodbwin321.6.0/data
不要關閉該 DOS 視窗。
mongoDB 服務端的預設串連連接埠是 27017。
4,mongoDB 服務端啟動後,重新開啟一個 DOS 視窗,進入D:/MongoDB/mongodbwin321.6.0/bin 目錄,運行命令 mongo.exe,將會出現下面的資訊:
D:/MongoDB/mongodbwin321.6.0/bin>mongo
MongoDB shell version: 1.4.0
url: test
connecting to: test
type "exit" to exit
type "help" for help
>
mongo.exe 是官方內建的一個命令列管理用戶端,在這裡可以進行資料庫管理和資料庫系統的維護。
5,運行:
>help 是協助命令
> show dbs;顯示所有資料庫顯示了 mongoDB 預置的幾個資料庫。
> use test;開啟資料庫
> show collections;顯示collection
> db.test.save({rpg:100});向 collection test 中儲存一條資訊
> db.test.find() ;檢索所有記錄
{ "_id" : ObjectId("5c558875dd6f010304531637"), "rpg" : 100}
二. Java Mongodb測試
1. 下載驅動包:mongo-java-dirver
2.
// 獲得資料庫服務
Mongo m = null;
try {
m = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
// 得到資料庫mytest
DB db = m.getDB("mytest");
DBCollection collection = db.getCollection("test");
BasicDBObject bdo = new BasicDBObject();
bdo.put("key1", "value1");
bdo.put("key2", "value2");
bdo.put("key3", "value3");
bdo.put("key4", "value4");
bdo.put("key5", "value5");
//不會插入重複記錄
bdo.put("key1", "value1");
//刪除所有的資料
collection.remove(new BasicDBObject());
collection.insert(bdo);
// 得到mytest資料庫下所有表名
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
mongoDB提供了 將資料匯出為 json 或者CSV格式的檔案的功能
具體見 http://www.mongodb.org/display/DOCS/Import+Export+Tools
其中需要注意的是:
If you want to output CSV, you have to specify the fields in the order you want them.
一個例子如下:
Java代碼
1.mongoexport -d dba22 -c foo -f x,y,a,b,c --csv -o ./result.csv
./mongoimport -d foo -c t1 /data/t1.json
-d 標示 資料庫
-c 標示 資料表
-f 需要提取的field用逗號分隔
-o 輸出路徑
echo "/usr/local/server/mongodb/bin/mongod --dbpath=/usr/local/server/mongodb/data –-logpath=/usr/local/server/mongodb/logs –-logappend --auth –-port=27017" >> /etc/rc.local
./bin/mongod --dbpath=/usr/local/server/mongodb/data --logpath=/usr/local/server/mongodb/logs --logappend --port=27017 --fork
package cn.wang.mongodb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import cn.wang.mongodb.beans.ImageBean;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
public class MongoDbTest {
public static void main(String[] args) {
// run();
List<ImageBean> images = getImageByType("1");
if (null == images) {
System.out.println("無記錄");
} else {
for (ImageBean image : images) {
System.out.println(image);
}
}
// //db.authenticate("opzoon", "123@opzoon.com".toCharArray());
//
// // 查詢所有的Database
// for (String name : m.getDatabaseNames()) {
// System.out.println("dbName: " + name);
// }
//
// DBCollection collection = db.getCollection("test");
//
// BasicDBObject bdo = new BasicDBObject();
// bdo.put("key1", "value1");
// bdo.put("key2", "value2");
// bdo.put("key3", "value3");
// bdo.put("key4", "value4");
// bdo.put("key5", "value5");
//
// // 不會插入重複記錄
// bdo.put("key1", "value1");
//
// // 刪除所有的資料
// collection.remove(new BasicDBObject());
// collection.insert(bdo);
// db.authenticate("opzoon", "opzoon".toCharArray());
// DBCollection collection = db.getCollection("test1");
// BasicDBObject data1 = new BasicDBObject();
// data1.put("key1", "value1");
// data1.put("key2", "value2");
// data1.put("key3", "value3");
// data1.put("key4", "value4");
// data1.put("key5", "value5");
// data1.put("key1", "value1");
//
// BasicDBObject data2 = new BasicDBObject();
// data2.put("key11", "value11");
// data2.put("key21", "value21");
// data2.put("key31", "value31");
// data2.put("key41", "value41");
// data2.put("key51", "value51");
// data2.put("key11", "value11");
//
// //刪除所有的資料
// collection.remove(new BasicDBObject());
// collection.insert(data1);
// collection.insert(data2);
//
//
// BasicDBObject data3 = new BasicDBObject();
// data3.put("aaa", "bbb");
// data3.put("ccc", "ddd");
// collection.update(new BasicDBObject().append("key11", "value11"),
// data3);
// DBCursor cursor = db.getCollection("test").find();
// while (cursor.hasNext()) {
// System.out.println(cursor.next());
// }
// 得到mytest資料庫下所有表名
// Set<String> colls = db.getCollectionNames();
// for (String s : colls) {
// System.out.println(s);
// }
}
public static void run() {
File imagesFile = new File("D:\\images.txt");
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(imagesFile);
br = new BufferedReader(fr);
String line;
while (null != (line = br.readLine())) {
insertMong(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
br = null;
}
if (null != fr) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
fr = null;
}
}
}
private static void insertMong(String line) {
String[] data = line.split(",");
String type = data[0];
String typeCh = data[1];
String theme = data[2];
String themeCh = data[3];
String name = data[4];
String remarks = data[5];
DB db = getMongoDb();
DBCollection collection = db.getCollection("image");
BasicDBObject bdo = new BasicDBObject();
bdo.put("type", type);
bdo.put("typech", typeCh);
bdo.put("theme", theme);
bdo.put("themech", themeCh);
bdo.put("name", name);
bdo.put("remarks", remarks);
// 不會插入重複記錄
// bdo.put("key1", "value1");
collection.insert(bdo);
}
private static DB getMongoDb() {
// 獲得資料庫服務
Mongo m = null;
try {
m = new Mongo("172.16.101.253", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
// 得到資料庫mytest
DB db = m.getDB("images");
return db;
}
public static List<ImageBean> getImageByType(String type) {
List<ImageBean> images = new ArrayList<ImageBean>();
DB db = getMongoDb();
DBCollection coll = db.getCollection("image");
DBObject obj = null;
DBCursor cursor = null;
if (null != type) {
obj = new BasicDBObject();
obj.put("type", type);
cursor = coll.find(obj).limit(6).skip(0);
} else {
cursor = coll.find();
}
// 分頁查詢
System.out.println(cursor.count());
while (cursor.hasNext()) {
ImageBean image = new ImageBean();
DBObject dbo = cursor.next();
String data = JSON.serialize(dbo);
System.out.println(data);
// 把字串變成對象
parseData(data, image);
images.add(image);
}
return images.isEmpty() ? null : images;
}
private static void parseData(String data, ImageBean image) {
StringBuffer sb = new StringBuffer(data);
sb.deleteCharAt(0).deleteCharAt(sb.length() - 1);
data = sb.toString();
String[] datas = data.split(",");
String type = datas[1];
String theme = datas[2];
String name = datas[3];
String remarks = datas[4];
image.setType(type);
image.setTheme(theme);
image.setName(name);
image.setRemarks(remarks);
}
}
package cn.wang.mongodb.beans;
import java.io.Serializable;
public class ImageBean implements Serializable{
private static final long serialVersionUID = -1962370938260404035L;
private int id;
private String name;
private String theme;
private String remarks;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
return "type: " + this.type + ", "
+ "theme: " + this.theme + ", "
+ "name: " + this.name + ", "
+ "remarks: " + this.remarks;
}
}