mongoDB是一種 NoSQL 的文檔型的數(shù)據(jù)庫管理系統(tǒng),也就是說不是傳統(tǒng)意義上的關(guān)系數(shù)據(jù)庫(類似Oracle、MS-SQLServer、MySQL等)。傳統(tǒng)意義上的關(guān)系數(shù)據(jù)庫,數(shù)據(jù)是被編碼為二進制格式保存在表中的,需要用 SQL 語句去存取。NoSQL 的文檔型數(shù)據(jù)庫,比如 mongoDB,就不同了。
1.下載Windows版本MongoDB:
最新版本是2.2.3
下載地址:http://www.ksks6.com/soft/34691.html
將其解壓至D:\mongodb目錄中.創(chuàng)建D:\mongodb\data目錄(用于存放數(shù)據(jù)庫文件).
注:BIN目錄中有以下2個文件,mongod.exe即服務(wù)器端,mongo.exe客服端(在php中用來連接mongodb它進行查詢)
2.運行
執(zhí)行windows cmd命令并輸入
mongod.exe --dbpath D:\web\sh2999mongodb\
注意上面的--depath是兩次"-"
提示類似以下信息:
Sun Feb 07 14:19:28 Mongo DB : starting : pid = 0 port = 27017 dbpath = D:\web\s
h2999mongodb\ master = 0 slave = 0 32-bit
** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more
Sun Feb 07 14:19:28 db version v1.2.2, pdfile version 4.5
Sun Feb 07 14:19:28 git version: 8a4fb8b1c7cb78648c55368d806ba35054f6be54
Sun Feb 07 14:19:28 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_LI
B_VERSION=1_35
Sun Feb 07 14:19:28 waiting for connections on port 27017
Sun Feb 07 14:20:36 connection accepted from 127.0.0.1:2574 #1
3.使用mongo命令
保持mongod命令窗口的運行狀態(tài),再新開一命令窗口
輸入E:\MyProgram\mongodb-win32-i386-1.8.2\bin\mongo,出現(xiàn)
MongoDB shell version:1.8.2
connecting to:test
>
OK,連接成功。
1,show dbs 顯示數(shù)據(jù)庫列表
2,use Northwind 進入Northwind數(shù)據(jù)庫,大小寫敏感
3,show collections 顯示數(shù)據(jù)庫中的集合
4,db 用于查看當(dāng)前所在的數(shù)據(jù)庫
5,db.Customer.count() 查看集合Customer的記錄總數(shù)
6,db.Customer.findOne({"_id":"1"}) 查看CustomerId=1的記錄
4.繼續(xù)打開一個新的CMD命令窗口
執(zhí)行mongo命令后
輸入以下新增
db.postlist.save({"Test":2999})
db,表示當(dāng)前數(shù)據(jù)庫。postlist,是一個集合,可以理解為一張名為postlist的表。
在postlist表中保存了一條記錄,記錄的字段Test的值為2999。
查看記錄請輸入
db.postlist.findOne()
有類似如下提示
{ "_id" : ObjectId("4b6e5ebed04f0000000006db"), "Test" : 2999 }
數(shù)據(jù)保存成功了。mongoDB會給每一個collection中的項自動分配一個_id值,
可以根據(jù)這個id來刪除、更新記錄。這個id是全局唯一的,系統(tǒng)會對這個_id自動作索引,guid,同步在兩個系統(tǒng)中,
記錄默認(rèn)還是是按插入時間來排序的。
5.新增數(shù)據(jù)
1,use MyTest,這個數(shù)據(jù)庫不存在,無所謂,mongo會創(chuàng)建,
MongoDB在使用前,并不要求您事先創(chuàng)建好相應(yīng)的數(shù)據(jù)庫,設(shè)計數(shù)據(jù)表結(jié)構(gòu)!
在MongoDB中,沒有【表】的概念,取而代之的是【集合】,也沒有【數(shù)據(jù)記錄】的概念,取而代之的是【文檔】,我們可以把【文檔】理解成一個【對象】,任意的對象,甚至可以有復(fù)雜的嵌套層次。
因此,我們不用再寫代碼從【數(shù)據(jù)表字段】到C#類的【屬性,字段】的轉(zhuǎn)換了,現(xiàn)在直接就可以讀寫整個對象了。
而且MongoDB不支持Join操作,所以,如果有【關(guān)聯(lián)】操作,就需要你自己來處理
2,item={"Key":"1","text":"wokao","number":3}
3,db.table1.insert(item),mongo將建立集合table1,并將item插入,完成了新增加數(shù)據(jù)庫的工作
4,db.table1.find()顯示table1中的數(shù)據(jù),MongoDB的文檔使用的是一種稱為BSON格式的對象,與Javascript中的JSON類似
5,額外的,輸入item1={"Id":5,"str":"asdfasdf"},再插入db.table1.insert(item1),再用find()命令看,也插入成功了~,注意到結(jié)構(gòu)和item不一樣!但不建議這樣做。
注意到:【每個文檔有一個名為 "_id" 的成員】,我可沒有定義啊。
其實,MongoDB會為每個文檔都創(chuàng)建這樣一個文檔成員,我們指定的 "key", "id" 對于MongoDB來說:它們并不是【文檔的主鍵】,MongoDB只認(rèn) "_id",你可以指定,但如果不指定,MongoDB就自動添加。
6.修改數(shù)據(jù)
1,var t=db.table1.findOne({"Id":5}),獲取一條記錄
2,t.str="wokao"
3,db.table1.update({"Id":5},t)
7.刪除數(shù)據(jù)
db.table1.remove({"Id":5})
查找數(shù)據(jù)
上面已經(jīng)有find和findOne命令,即用于查詢
db.table1.find()
MongoDB的查詢條件中,并沒有 >, <, >= , <= 這些運算符,而是使用 "$lt", "$lte", "$gt", "$gte"
新建表
db.MyTest.table2.save({})
刪除表
db.table1.drop()或db.runCommand({"drop","table1"})
刪除數(shù)據(jù)庫
db.runCommand({"dropDatabase": 1}),此命令只能刪除當(dāng)前數(shù)據(jù)庫
獲取服務(wù)端狀態(tài)信息
db.runCommand({"serverStatus" : 1})
8.運行mongoDB時的錯誤與處理:
完成安裝后,結(jié)束掉所有CMD窗口,然后進行如下操作:
1,運行E:\MyProgram\mongodb-win32-i386-1.8.2\bin\mongo,可能會報錯誤:couldn't connect to server 127.0.0.1 shell/mongo.js,原因是mongod.exe沒有啟動,
2,既然沒啟動,那咱就啟動唄,運行E:\MyProgram\mongodb-win32-i386-1.8.2\bin\mongod,可能會報錯誤:dbpath (/data/db/) does not exist, terminating,看這樣子,本人覺得還得在data文件夾下再建一個db文件夾啊,如此即新建一db文件夾,再運行mongod命令,結(jié)果提示一樣,不知道怎么搞啦,咱google吧
找到文章http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
說“To start Mongo in default mode, where data will be stored in the /data/db directory (or c:\data\db on Windows), and listening on port 27017”,哦,原來默認(rèn)的文檔路徑在c:\data\db,啥也不說,直接按這個建文件夾,再次運行命令mongod,OK了,啟動成功,這個命令窗口不能關(guān)閉,否則mongoDB就退出了
重點說明一下update
db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查詢條件,類似sql update查詢內(nèi)where后面的
objNew : update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內(nèi)set后面的
upsert : 這個參數(shù)的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認(rèn)是false,不插入。
multi : mongodb默認(rèn)是false,只更新找到的第一條記錄,如果這個參數(shù)為true,就把按條件查出來多條記錄全部更新。
db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一條記錄
db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加進去了第一條
db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加進去了
db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了
db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一條
對應(yīng)的SQL參考:
select a,b from users where age=33
db.users.find({age:33},{a:1,b:1})
select * from users where age>33 and age<=40
db.users.find({'age':{$gt:33,$lte:40}})
select * from users order by name DESC
db.users.find().sort({name:-1})
create index myindexname on users(name)
db.users.ensureIndex({name:1})
update users set a=1 where b='q'
db.users.update({b: 'q'},{$set:{a:1}},false,true)
delete from users where z='abc'
db.users.remove({ z: 'abc'})